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

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

问题描述

是否可以使用 json.net 从 json 反序列化中返回动态对象?我想做这样的事情:

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);

推荐答案

Json.NET 允许我们这样做:

Json.NET allows us to 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);

输出:

 1000
 string
 6

此处的文档:LINQ toJSON 与 Json.NET

Documentation here: LINQ to JSON with Json.NET

另见 JObject.ParseJArray.Parse

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

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