1

I'm trying to deserialize JSON into a custom object but all my properties are set to null and not sure what's going on. Does anyone see anything wrong?

{
categories: [
    {
        id: 1,
        parent_id: null,
        category_name: "Health",
        url: "uploads/categories/category5.png",
        file_name: "category5.png",
        created_at: "2018-08-06 09:52:45",
        updated_at: "2018-08-06 09:52:45",
        deleted_at: null
    }
],
items: [
    {
        id: 1,
        item_name: "Item",
        hits: 0,
        file_name: "12._dental_leaflet.pdf",
        file_url: "uploads/docs/12._dental_leaflet.pdf",
        thumb: "category4.png",
        thumb_url: "uploads/docs/thumb/category4.png",
        type: "pdf",
        category_id: 1,
        created_at: "2018-08-06 09:57:04",
        updated_at: "2018-08-06 09:57:04",
        deleted_at: null
    }
]
}

Here I have made three class. One of the categories and one for items. And Make a collection class to bind them. But What I am done wrong. I can not find where I am doing wrong. Please give me some solution. And help me to find out where I am doing wrong

class Category
    {
        int id;
        int parent_id;
        string category_name;
        string url;
        string file_name;
        string created_at;
        string updated_at;
        string deleted_at;

        public int Id { get => id; set => id = value; }
        public int Parent_id { get => parent_id; set => parent_id = value; }
        public string Category_name { get => category_name; set => category_name = value; }
        public string Url { get => url; set => url = value; }
        public string File_name { get => file_name; set => file_name = value; }
        public string Created_at { get => created_at; set => created_at = value; }
        public string Updated_at { get => updated_at; set => updated_at = value; }
        public string Deleted_at { get => deleted_at; set => deleted_at = value; }
    }


class Item
    {
        int id;
        int category_id;
        string item_name;
        string file_url;
        string thumb;
        string thumb_url;
        string type;
        string created_at;
        string updated_at;
        string deleted_at;

        public int Id { get => id; set => id = value; }
        public int Category_id { get => category_id; set => category_id = value; }
        public string Item_name { get => item_name; set => item_name = value; }
        public string File_url { get => file_url; set => file_url = value; }
        public string Thumb { get => thumb; set => thumb = value; }
        public string Thumb_url { get => thumb_url; set => thumb_url = value; }
        public string Type { get => type; set => type = value; }
        public string Created_at { get => created_at; set => created_at = value; }
        public string Updated_at { get => updated_at; set => updated_at = value; }
        public string Deleted_at { get => deleted_at; set => deleted_at = value; }
    }


class JsonCollection
    {
        private List<Category> categories;
        private List<Item> items;

        public List<Category> Categories { get => categories; set => categories = value; }
        public List<Item> Items { get => items; set => items = value; }

    }

And my deserialization code is

    WebClient webClient = new WebClient();

    string rawJson = webClient.DownloadString(url);
    JsonCollection jsonCollection = JsonConvert.DeserializeObject<JsonCollection>(rawJson);

Here the problem is jsonCollection shows null. I am new in C#. I am trying in a various way. It takes my whole day. I don't find where I am doing wrong. Please give me some solution.

Mahfuz Shishir
  • 841
  • 1
  • 7
  • 24
  • you don't need to do `public int Id { get => id; set => id = value; }` `public int Id {get; set;}` does the same thing with much less code – Liam Aug 06 '18 at 10:20
  • 2
    Your public properties `Categories` and `Items` start with an uppercase, while the json is all lowercase. – HHLV Aug 06 '18 at 10:21
  • 1
    Your property names don't match, your trying to turn deseailize `id` (Json) to `Id`(C#). If you want them to have different names you need to use `JsonProprty("id")`, etc – Liam Aug 06 '18 at 10:21
  • 1
    try to set [JsonProperty(PropertyName = "categories")] and [JsonProperty(PropertyName = "items")] in your private fields – Ion Cojucovschi Aug 06 '18 at 10:48
  • I have just added the property name in JsonCollection class [JsonProperty(PropertyName = "categories")] and [JsonProperty(PropertyName = "items")] – Mahfuz Shishir Aug 06 '18 at 11:41
  • Gotcha, so the same as the duplicate. – mjwills Aug 06 '18 at 12:00

0 Answers0