[英]How to deserialize the JSON object in c#
I have a following JSON code, 我有以下JSON代码,
{
"dataContainer":{
"sheets":{
"1":{
"0":{
"1":{
"type":6,
"value":2,
"value2":"11-15-1991"
},
"2":{
"type":10,
"value":3,
"value2":3
}
},
"1":{
"1":{
"type":6,
"value":7,
"value2":"11-16-1991"
},
"2":{
"type":10,
"value":8,
"value2":8
}
}
},
"2":{
"0":{
"1":{
"type":6,
"value":2,
"value2":"11-15-1991"
},
"2":{
"type":10,
"value":3,
"value2":3
}
},
"1":{
"1":{
"type":6,
"value":7,
"value2":"11-16-1991"
},
"2":{
"type":10,
"value":8,
"value2":8
}
}
}
}}
}
i need to deserialize that code, i have tried that one, 我需要反序列化该代码,我已经尝试过了,
Dictionary<string,object> dict = ser.Deserialize<Dictionary<string, object>>(obj);
List<object> sheetObject = new List<object>();
sheetObject = ((Dictionary<string, object>)dict["dataContainer"]).Values.ToList();
public object sheets = new object();
sheets = sheetObject[0];
but this is not working i want to able to work like below, 但这不起作用,我希望能够像下面这样工作,
sheets[1][0][1].type
how to achieve this? 如何做到这一点?
Json.NET is the library to use. Json.NET是要使用的库。 This library has a function which will take a JSON string and return a "dynamic" variable representing the JSON.
该库具有一个函数,该函数将接收JSON字符串并返回表示JSON的“动态”变量。 Syntax goes like this:
语法如下:
dynamic dict = JsonConvert.DeserializeObject(YOUR_JSON_STRING);
You can now access the entire JSON tree dynamically, eg: 您现在可以动态访问整个JSON树,例如:
dynamic sheetObject = dict.dataContainer.sheets;
To iterate over the children at any given level, just use a foreach: 要遍历任何给定级别的子级,只需使用foreach即可:
foreach (dynamic child in sheetObject.Children()) { /* do something */ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.