TPG
TPG

Reputation: 3209

How to deserialize JSON using JSON.Net

I have the following JSON data

[
  {
    "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"
      }
    ]
  }
]

And I have created similar Class for MainCategory and SubCategory as below:

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; }
    }

When I call List<MainCategory> mainCategories = JsonConvert.DeserializeObject<List<MainCategory>>(categoryJSON);, it did creates 2 MainCategory objects, however all the value inside is 'null'. I thought I have matched the name correctly, may I know if any experts could shed some light to me? Thanks.

Upvotes: 0

Views: 57

Answers (2)

John
John

Reputation: 351

Your class structure is correct. I have created class structure as following and it works.

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; }
}

Now deserialize using

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

You are missing public access modifier in your class property.

Generated class using http://json2csharp.com/

Upvotes: 1

TPG
TPG

Reputation: 3209

Got the answer, just need to add Public keyword to each of the variables, updated code as below.

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; }
    }

Upvotes: 1

Related Questions