JSON数字根到通用名称

时间:2015-03-27 13:15:19

标签: c# json vb.net

我有一个看起来像这样的JSON文件。

{
  "1000": {
    "id": 1000,
    "name": "Clothes, Shoes and Bags",
    "path_from_root": [
      {
        "id": 1000,
        "name": "Clothes, Shoes and Bags"
      }
    ],
    "children_categories": [
      {
        "id": 1001,
        "name": "Accessories"
      },
      {
        "id": 2422,
        "name": "Athletic Shoes"
      },
      {
        "id": 2303,
        "name": "Baby Clothes"
      },
      {
        "id": 2115,
        "name": "Backpacks"
      },
      {
        "id": 1071,
        "name": "Bags"
      },
      {
        "id": 2105,
        "name": "Bags and Purses"
      },
      {
        "id": 2087,
        "name": "Children's Outfits"
      },
      {
        "id": 2051,
        "name": "Coats, Jackets and Vests"
      },
      {
        "id": 3192,
        "name": "Dresses and Skirts"
      },
      {
        "id": 3336,
        "name": "Glasses"
      },
      {
        "id": 2249,
        "name": "Others"
      },
      {
        "id": 2092,
        "name": "Overalls"
      },
      {
        "id": 1366,
        "name": "Pants, Shorts & Bermudas"
      },
      {
        "id": 3191,
        "name": "School Uniforms"
      },
      {
        "id": 1894,
        "name": "Shirts"
      },
      {
        "id": 2353,
        "name": "Shoes"
      },
      {
        "id": 2417,
        "name": "Suits"
      },
      {
        "id": 2139,
        "name": "Swimwear"
      },
      {
        "id": 1979,
        "name": "T-shirts and Blouses"
      },
      {
        "id": 2254,
        "name": "Underwear and Lingerie"
      }
    ],
    "attributes_required": false,
    "max_pictures_per_item": 12,
    "max_title_length": 60,
    "max_price": null,
    "min_price": null,
    "listing_allowed": false
  },
  "1001": {
    "id": 1001,
    "name": "Accessories",
    "path_from_root": [
      {
        "id": 1000,
        "name": "Clothes, Shoes and Bags"
      },
      {
        "id": 1001,
        "name": "Accessories"
      }
    ],
    "children_categories": [
      {
        "id": 1055,
        "name": "Boy"
      },
      {
        "id": 1022,
        "name": "For Men"
      },
      {
        "id": 1002,
        "name": "For Women"
      },
      {
        "id": 1038,
        "name": "Girl"
      }
    ],
    "attributes_required": false,
    "max_pictures_per_item": 12,
    "max_title_length": 60,
    "max_price": null,
    "min_price": null,
    "listing_allowed": false
  },
  "1002": {
    "id": 1002,
    "name": "For Women",
    "path_from_root": [
      {
        "id": 1000,
        "name": "Clothes, Shoes and Bags"
      },
      {
        "id": 1001,
        "name": "Accessories"
      },
      {
        "id": 1002,
        "name": "For Women"
      }
    ],
    "children_categories": [
      {
        "id": 1011,
        "name": "Beanies"
      },
      {
        "id": 1008,
        "name": "Belly Dance"
      },
      {
        "id": 1007,
        "name": "Belts"
      },
      {
        "id": 1003,
        "name": "Berets"
      },
      {
        "id": 1004,
        "name": "Caps"
      },
      {
        "id": 1014,
        "name": "Gloves"
      },
      {
        "id": 1006,
        "name": "Hats"
      },
      {
        "id": 1013,
        "name": "Neckerchiefs"
      },
      {
        "id": 1015,
        "name": "Others"
      },
      {
        "id": 1016,
        "name": "Pashmina Scarves"
      },
      {
        "id": 1005,
        "name": "Scarves"
      },
      {
        "id": 1017,
        "name": "Semi Precious Jewelry"
      },
      {
        "id": 1009,
        "name": "Shawls"
      },
      {
        "id": 1020,
        "name": "Ski Caps"
      },
      {
        "id": 1010,
        "name": "Stoles"
      },
      {
        "id": 1018,
        "name": "Suspenders"
      },
      {
        "id": 1019,
        "name": "Tiaras"
      },
      {
        "id": 1012,
        "name": "Ties"
      },
      {
        "id": 1021,
        "name": "Veils"
      }
    ],
    "attributes_required": false,
    "max_pictures_per_item": 12,
    "max_title_length": 60,
    "max_price": null,
    "min_price": null,
    "listing_allowed": false
  },
  "1003": {
    "id": 1003,
    "name": "Berets",
    "path_from_root": [
      {
        "id": 1000,
        "name": "Clothes, Shoes and Bags"
      },
      {
        "id": 1001,
        "name": "Accessories"
      },
      {
        "id": 1002,
        "name": "For Women"
      },
      {
        "id": 1003,
        "name": "Berets"
      }
    ],
    "children_categories": [],
    "attributes_required": false,
    "max_pictures_per_item": 12,
    "max_title_length": 60,
    "max_price": null,
    "min_price": null,
    "listing_allowed": false
  }
  }

如果你看到,根名称就像

1000
1001
1002
1003

我想将它DeSerialize为一个对象。这些数字下方的Strcuture是相同的。

有人可以建议我如何为此生成课程?和使用NewtonSoft反序列化?

注意,我无法更改JSON,它的大文件(大约16MB)。

2 个答案:

答案 0 :(得分:2)

有一个很酷的功能叫做将JSON粘贴为类

enter image description here

here。注意:这是originally came with visual studio扩展 web essenstials 的功能。因此,根据您的VS版本,您可能需要install this extension


如果粘贴第一个根节点,它会生成以下类:

public class Rootobject
{
    public _1000 _1000 { get; set; }
}

public class _1000
{
    public int id { get; set; }
    public string name { get; set; }
    public Path_From_Root[] path_from_root { get; set; }
    public Children_Categories[] children_categories { get; set; }
    public bool attributes_required { get; set; }
    public int max_pictures_per_item { get; set; }
    public int max_title_length { get; set; }
    public object max_price { get; set; }
    public object min_price { get; set; }
    public bool listing_allowed { get; set; }
}

public class Path_From_Root
{
    public int id { get; set; }
    public string name { get; set; }
}

public class Children_Categories
{
    public int id { get; set; }
    public string name { get; set; }
}

这可能是,你需要的。也许稍微改进一下。

答案 1 :(得分:1)

字典怎么样? Post标记为VB和C#,这是VB:

Public Class Item
    Public Property id As Integer
    Public Property name As String
    Public Property path_from_root As NameValuePair()
    Public Property children_categories As NameValuePair()
    Public Property attributes_required As Boolean
    Public Property max_pictures_per_item As Integer
    Public Property max_title_length As Integer
    Public Property max_price As Object
    Public Property min_price As Object
    Public Property listing_allowed As Boolean
End Class

Public Class NameValuePair
    Public Property id As Integer
    Public Property name As String
End Clas

...
Dim jstr As String = from whereever

Dim jd = JsonConvert.DeserializeObject(Of Dictionary(Of Integer, Item))(jstr)

这样,您就可以按键/ ID访问所有信息。例如:

Console.WriteLine("id/key: {0} name: '{1}' path0: '{2}'", jd(1003).id.ToString(),
                  jd(1003).name, jd(1003).path_from_root(0).name)

输出:

  

id / key:1003名称:'Berets'path0:'衣服,鞋子和包包'


请注意,根据提供的数据,max_pricemin_price是对象。如果它们有时被使用并且是数字的,Nullable(Of Double)可能会更好。