{
"id":"123", "result":
{
"0": { "children_id": "0", "name": "some name" },
"1": { "children_id": "1", "name": "some other name" }
}
}
我有这个JSON字符串,我可以使用以下方法对其进行反序列化: JSON.NET http://json.codeplex.com?
我正在尝试制作一些可以容纳我的json对象的类,但我不知道我怎么能保持这个“0”和“1”。
public class Data
{
public string children_id { get; set; }
public string name { get; set; }
}
public class RootObject
{
public string id { get; set; }
public List<Data> result { get; set; }
}
答案 0 :(得分:6)
这个JSON看起来很糟糕。如果您可以有多个子节点,则它应该是一个数组,例如:
{
"id":"123", "result":
[
{ "children_id": "0", "name": "some name" },
{ "children_id": "1", "name": "some other name" }
]
}
如果您可以控制JSON生成,请更改它(您当前的C#代码将在反序列化中起作用)。如果不这样做,最好将其反序列化为字典,这会使RootObject
看起来像这样:
public class RootObject
{
public string id { get; set; }
public Dictionary<int, Data> result { get; set; }
}
答案 1 :(得分:1)
public class Data
{
public string id { get; set; }
public string name { get; set; }
}
public class RootObject
{
public string id { get; set; }
public Dictionary<string, Data> result { get; set; }
}
答案 2 :(得分:-1)
JObject jObject = new JObject();
/* jObject = {
"id":"123", "result":
{
"0": { "children_id": "0", "name": "some name" },
"1": { "children_id": "1", "name": "some other name" }
}
}
*/
foreach(var g in jObject["result"]){
switch(g.Key()){
case 0://do something
break;
}
}