如何使用json.net返回嵌套json对象:
我如何设置我的类返回这个
[ {
"name": "Jasmine Coots",
"sex": "female",
"age": 49,
"Sons": [
{
"name": "Morgan Rhett",
"age": 25,
"sex": "male"
},
{
"name": " Jasmine Coots ",
"age": 22,
"sex": "female"
},
{
"name": "Joaquin Strother",
"age": 17,
"sex": "male"
}
]
},
{
"name": "Jasmine Coots",
"sex": "female",
"age": 49,
"Sons": [
{
"name": "Morgan Rhett",
"age": 25,
"sex": "male"
},
{
"name": " Jasmine Coots ",
"age": 22,
"sex": "female"
},
{
"name": "Joaquin Strother",
"age": 17,
"sex": "male"
}
]
}
]
我需要在c#中使用列表和数据表或数据阅读器返回一个对象数组,任何如何做到这一点的例子都将是伟大的
生活小贴士:看看这个网站,它太棒了。我唯一不太喜欢的是,它不会自动使用传统的属性名(PascalCase),但它真的很方便。只要拼写正确,您可以自己简单地进行调整。
现在,使用它你可以生成你需要反序列化的类。
public class Son
{
public string name { get; set; }
public int age { get; set; }
public string sex { get; set; }
}
public class RootObject
{
public string name { get; set; }
public string sex { get; set; }
public int age { get; set; }
public List<Son> Sons { get; set; }
}
用法:
var jsonString = "this is your JSON data";
var root = JsonConvert.DeserializeObject<RootObject>(jsonString);
// do stuff