如何使用JSON.Net反序列化JSON



我有以下JSON数据

[
  {
    "Id": "a038ca5f-8ef6-41c1-a610-d76c26517058",
    "MainCategoryName": "Digital",
    "LastPublished": "20130731T15:09:51",
    "SubCategories": [
      {
        "Id": "39e4e9c2-0870-4906-99a4-938bf149a138",
        "SubCategoryName": "Electrical and Electronics",
        "LastPublished": "20130729T11:30:27"
      },
      {
        "Id": "41d23a7d-d70f-4e41-9cd3-ddede6f17370",
        "SubCategoryName": "Information Technology",
        "LastPublished": "20130814T16:55:53"
      },
      {
        "Id": "72d6904c-39be-4cf5-810a-e01e6df2b6a9",
        "SubCategoryName": "Telecommunications",
        "LastPublished": "20141209T15:02:07"
      }
    ]
  },
  {
    "Id": "536d443d-7a77-48b1-9721-0adabd4d11e7",
    "MainCategoryName": "Dining",
    "LastPublished": "20130729T10:38:25",
    "SubCategories": [
      {
        "Id": "e8a6b2af-188e-4143-a30f-15816ba10715",
        "SubCategoryName": "Cafe",
        "LastPublished": "20130828T17:02:23"
      },
      {
        "Id": "7fa05929-01f1-4aa5-bb0a-007bf5d75ca1",
        "SubCategoryName": "Confectionery and Specialty",
        "LastPublished": "20140715T17:16:08"
      },
      {
        "Id": "33470a6a-aa8b-4842-8ce7-57bb0f6c77ac",
        "SubCategoryName": "Delivery",
        "LastPublished": "20140715T17:17:40"
      },
      {
        "Id": "b5919130-5f78-4632-93e2-5bcedf663744",
        "SubCategoryName": "Dessert Bar",
        "LastPublished": "20150424T12:46:20"
      },
      {
        "Id": "e93d9457-f93e-4d23-b412-ffa9996ef8e1",
        "SubCategoryName": "Fast Food",
        "LastPublished": "20140715T17:18:58"
      },
      {
        "Id": "0860e413-f8fc-48aa-a24d-9f8e7eb98abd",
        "SubCategoryName": "Food Kiosk and Takeaway",
        "LastPublished": "20140715T17:19:23"
      },
      {
        "Id": "1188c663-2ea3-42a3-991c-9c9855801265",
        "SubCategoryName": "Foodcourt",
        "LastPublished": "20140715T17:19:31"
      },
      {
        "Id": "3bfbb105-3582-482e-a333-d987dc9bd70c",
        "SubCategoryName": "Pub and Bar and Lounge",
        "LastPublished": "20141027T12:02:42"
      },
      {
        "Id": "ed49e69d-38c6-4ebf-a844-03fab83854f7",
        "SubCategoryName": "Restaurant",
        "LastPublished": "20140715T17:20:02"
      },
      {
        "Id": "d7ae7119-7cd5-47c9-aa22-9094a91ac1bc",
        "SubCategoryName": "Restaurant and Bar",
        "LastPublished": "20140715T17:20:24"
      },
      {
        "Id": "ce0704aa-afcf-49ba-8d73-09ebe1f59624",
        "SubCategoryName": "Restaurant with Live Band",
        "LastPublished": "20140715T17:20:42"
      },
      {
        "Id": "3896e1bb-82aa-4866-a001-3d2c0d6f704b",
        "SubCategoryName": "Quick Bite",
        "LastPublished": "20141027T17:26:49"
      },
      {
        "Id": "e088dbc6-e4c0-4e39-9ba6-7cc46567408a",
        "SubCategoryName": "Asian Delights",
        "LastPublished": "20140715T17:20:50"
      },
      {
        "Id": "f0b6c043-60fc-46d7-b9dd-aa2f9f8a682b",
        "SubCategoryName": "Chinese Cuisine",
        "LastPublished": "20140715T17:16:14"
      },
      {
        "Id": "c4498d9d-b747-401d-a666-3c59819f44b7",
        "SubCategoryName": "Western Delicacies",
        "LastPublished": "20140715T17:16:16"
      }
    ]
  }
]

我为MainCategory和SubCategory创建了类似的类,如下所示:

public class MainCategory
    {
        string Id { get; set; }
        string MainCategoryName { get; set; }
        string LastPublished { get; set; }
        IList<SubCategory> SubCategories { get; set; }
    }
public class SubCategory
    {
        string Id { get; set; }
        string SubCategoryName { get; set; }
        string LastPublished { get; set; }
    }

当我调用List<MainCategory> mainCategories = JsonConvert.DeserializeObject<List<MainCategory>>(categoryJSON);时,它确实创建了2个MainCategory对象,但里面的所有值都是"null"。我想我已经正确地匹配了这个名字,我可以知道是否有专家可以为我提供一些线索吗?谢谢

得到了答案,只需要将Public关键字添加到每个变量中,更新代码如下。

public class MainCategory
    {
        public string Id { get; set; }
        public string MainCategoryName { get; set; }
        public string LastPublished { get; set; }
        public List<SubCategory> SubCategories { get; set; }
    }
public class SubCategory
    {
        public string Id { get; set; }
        public string SubCategoryName { get; set; }
        public string LastPublished { get; set; }
    }

您的类结构是正确的。我创建了如下的类结构,它是有效的。

public class SubCategory
{
    public string Id { get; set; }
    public string SubCategoryName { get; set; }
    public string LastPublished { get; set; }
}
public class RootObject
{
    public string Id { get; set; }
    public string MainCategoryName { get; set; }
    public string LastPublished { get; set; }
    public List<SubCategory> SubCategories { get; set; }
}

现在使用进行反序列化

List<RootObject> mainCategories = JsonConvert.DeserializeObject<List<RootObject>>(categoryJSON);

类属性中缺少public访问修饰符。

使用http://json2csharp.com/ 生成的类

相关内容

  • 没有找到相关文章

最新更新