When processing JSON data, sometimes it is helpful to serialize the data into a dynamic object. This allows you to access attributes without the need to explicitly specify their types.
Use dynamic back serialization
JSON.NET provides the function of using Dynamic keywords to sequence JSON's back series into dynamic objects:
This code assumes that the JSON string JSON contains attributes called MESSAGE.
dynamic jsonResponse = JsonConvert.DeserializeObject(json); Console.WriteLine(jsonResponse.message);
Consider the following JSON data:
You can sequence this JSON's back series into dynamic objects as follows:
{ "number": 1000, "str": "string", "array": [1,2,3,4,5,6] }
dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}"); Console.WriteLine(d.number); // 输出:1000 Console.WriteLine(d.str); // 输出:string Console.WriteLine(d.array.Count); // 输出:6
The above is the detailed content of How Can I Dynamically Deserialize JSON in C# Using Json.Net?. For more information, please follow other related articles on the PHP Chinese website!