In the .NET programming,
is usually used to serve serialized JSON content. However, creating a custom class for each JSON object may be cumbersome. In order to simplify this process, JSON can be serialized into a C#dynamic type.
DataContractJsonSerializer
JSON.NET provides a convenient way to sequence JSON's derivatives into dynamic objects:
This code turns JSON back order into a dynamic object named "Stuff". You can then use point representation to directly access its attributes.
<code class="language-csharp">dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }"); string name = stuff.Name; string address = stuff.Address.City;</code>
newtonsoft.json.linq also provides a way to analyze JSON as dynamic objects:
This code uses to create afrom JSON, and then you can access it as a dynamic object.
<code class="language-csharp">dynamic stuff = JObject.Parse("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }"); string name = stuff.Name; string address = stuff.Address.City;</code>
JObject.Parse
JObject
For more information about this topic, please refer to the following documents:
json.net uses a dynamic object to query json
The above is the detailed content of How to Deserialize JSON into C# Dynamic Objects?. For more information, please follow other related articles on the PHP Chinese website!