且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

反序列化JSON对象为使用Json.net动态对象

更新时间:2023-01-17 10:34:33

最新版本json.net允许这样做:

 动态D = JObject.Parse({编号:1000,STR:'串',数组:[1,2,3,4,5,6]});Console.WriteLine(d.number);
Console.WriteLine(d.str);
Console.WriteLine(d.array.Count);

输出:

  1000
 串
 6

在这里的文档: 的LINQ to JSON与Json.NET

Is it possible to return a dynamic object from a json deserialization using json.net? I would like to do something like this:

dynamic jsonResponse = JsonConvert.Deserialize(json);
Console.WriteLine(jsonResponse.message);

latest json.net version allow do this:

dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");

Console.WriteLine(d.number);
Console.WriteLine(d.str);
Console.WriteLine(d.array.Count);

output:

 1000
 string
 6

Documentation here: LINQ to JSON with Json.NET