c#newtonsoft解析内部数组

时间:2016-09-15 01:21:40

标签: c# json c#-4.0

我将我的json deserialaize到一个模型类很好但我想在保存到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

1 个答案:

答案 0 :(得分:0)

  
      
  1. 为JSON对象创建模型。 (http://json2csharp.com/
  2.   
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; }
}
  
      
  1. 使用Json.NET JSON反序列化器,
  2.   
RootObject obj = JsonConvert.DeserializeObject<RootObject>("your json string");