c# newtonsoft解析内部数组
本文关键字:内部 数组 newtonsoft | 更新日期: 2023-09-27 18:07:13
我反序列化我的json到一个模型类很好,但我想在保存到sqlite之前做一些关于盒子数组的工作,我被困在循环通过所有的盒子,并获得值。
{
"data": [ // single outer array
{
"id": 8620379,
"business_id": 191,
"business_name": "yada",
"boxes": [
{
"box_id": 485,
"box_name": "5/6",
"box_group": null
},
{
"box_id": 483,
"box_name": "1/2",
"box_group": null
},
{
"box_id": 484,
"box_name": "3/4",
"box_group": null
}
]
},
{
"id": 8636759,
"business_id": 257,
"business_name": "something else",
"boxes": [
{
"box_id": 1176,
"box_name": "FC",
"box_group": null
}
]
}, // and more boxes
为JSON对象创建一个模型。(http://json2csharp.com/)
public class Box
{
public int box_id { get; set; }
public string box_name { get; set; }
public object box_group { get; set; }
}
public class Datum
{
public int id { get; set; }
public int business_id { get; set; }
public string business_name { get; set; }
public List<Box> boxes { get; set; }
}
public class RootObject
{
public List<Datum> data { get; set; }
}
- 使用Json。. NET JSON反序列化器,
RootObject obj = JsonConvert.DeserializeObject<RootObject>("your json string");