使用Newtonsoft JSON.NET对动态Json字符串进行反序列化 [英] Deserialize Dynamic Json string using Newtonsoft JSON.NET
问题描述
它可以是任何数字
我想要它的值。如何得到它?
示例:
{
data:[
{
id:1492292372_10201810786059989,
created_time:2014-04-05T09:00:54 + 0000
},
{
id:1492292372_10201804679827337 ,
created_time:2014-04-04T07:29:07 + 0000
},
{
id:1492292372_10201804649306574,
created_time:2014-04-04T07:10:33 + 0000
},
{
id:1492292372_10201801316823264,
created_time:2014- 04-03T18:31:50 + 0000
},
{
id:1492292372_10201798962284402,
created_time:2014-04-03T06:24: 47 + 0000
},
{
message_tags:{
0:[
{
id:1492292372
name:Yawar Sohail,
type:user,
offset:0,
length:12
}
],
15:[
{
id:1489845168,
name:Zeeshan Anjum,
type :user,
offset:15,
length:13
}
]
},
id:1492292372_10201796274777216,
created_time :2014-04-02T17:57:05 + 0000
},
{
id:1492292372_10201794080482360,
created_time:2014-04- 02T07:26:23 + 0000
},
在message_tags里面有两个节点[ 0和15],它们根据其偏移值动态地改变。我需要这些节点中的名称,类型和ids。
您可以将JSON反序列化为 ExpandoObject :
var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject< ExpandoObject>(json,converter);
在运行时动态添加成员到您的对象,并允许您迭代他们本答案中所述:
foreach(var prop in obj.GetType()。GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
Console.WriteLine(Name:{0},Value:{1},prop.Name,prop.GetValue(obj,null));
}
这样可以迭代 obj.message_tags
来获取各自的消息,并分别获取所有的细节。
I have a JSON string that I'm getting from Facebook API, in which I have a node whose name changes according to its content, for example some time it is 45, or 58 etc. It could be any number. I want its value. How to get it? Example:
{
"data": [
{
"id": "1492292372_10201810786059989",
"created_time": "2014-04-05T09:00:54+0000"
},
{
"id": "1492292372_10201804679827337",
"created_time": "2014-04-04T07:29:07+0000"
},
{
"id": "1492292372_10201804649306574",
"created_time": "2014-04-04T07:10:33+0000"
},
{
"id": "1492292372_10201801316823264",
"created_time": "2014-04-03T18:31:50+0000"
},
{
"id": "1492292372_10201798962284402",
"created_time": "2014-04-03T06:24:47+0000"
},
{
"message_tags": {
"0": [
{
"id": "1492292372",
"name": "Yawar Sohail",
"type": "user",
"offset": 0,
"length": 12
}
],
"15": [
{
"id": "1489845168",
"name": "Zeeshan Anjum",
"type": "user",
"offset": 15,
"length": 13
}
]
},
"id": "1492292372_10201796274777216",
"created_time": "2014-04-02T17:57:05+0000"
},
{
"id": "1492292372_10201794080482360",
"created_time": "2014-04-02T07:26:23+0000"
},
Inside message_tags there are two nodes [0 and 15] they dynamically changes according to their offset values. I want names, type and ids inside these nodes.
You can deserialize your JSON into an ExpandoObject:
var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);
Which dynamically adds members to your object at runtime, and allows you to iterate over them as described in this answer:
foreach (var prop in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
Console.WriteLine("Name: {0}, Value: {1}",prop.Name, prop.GetValue(obj,null));
}
That way you can iterate over obj.message_tags
to get the individual messages, and obtain all their details respectively.
这篇关于使用Newtonsoft JSON.NET对动态Json字符串进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!