反序列化JSON对象为使用Json.net动态对象 [英] Deserialize json object into dynamic object using Json.net

查看:102
本文介绍了反序列化JSON对象为使用Json.net动态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用json.net一个JSON反序列化返回一个动态对象?我愿做这样的事情:

 动态jsonResponse = JsonConvert.Deserialize(JSON);
Console.WriteLine(jsonResponse.message);


解决方案

最新版本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

这篇关于反序列化JSON对象为使用Json.net动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆