skip to Main Content

I have a JSON data, which I want to Deserialize and load.

I’m able to Deserialize and load with partial data, but not with all.
Below is the code and the JSON data.

public class Category
{
public string CategoryID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string ParentCategoryID { get; set; }
public string CountDiscussions { get; set; }
public string CountComments { get; set; }
public JObject Children { get; set; }
}

public class ChildCategory
{
public int CategoryID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int ParentCategoryID { get; set; }
public int CountDiscussions { get; set; }
public int CountComments { get; set; }
public JArray Children { get; set; }
}

public class Program
{
public static void Main()
{
    string jsonData = @"[
        {   
    ""categoryID"": 93,
    ""name"": ""Cloths"",
    ""description"": """",
    ""parentCategoryID"": null,
    ""countDiscussions"": 10,
    ""countComments"": 20,
    ""children"": {
        ""57"": {
            ""categoryID"": 180,
            ""name"": ""Shirt"",
            ""description"": """",
            ""parentCategoryID"": 9,
            ""countDiscussions"": 2,
            ""countComments"": 4,
            ""children"": []
        },
        ""56"": {
            ""categoryID"": 187,
            ""name"": ""ShirtAAAAA"",
            ""description"": """",
            ""parentCategoryID"": 9,
            ""countDiscussions"": 2,
            ""countComments"": 4,
            ""children"": []
        }
    }
},
{
    ""categoryID"": 172,
    ""name"": ""accessories"",
    ""description"": """",
    ""parentCategoryID"": null,
    ""countDiscussions"": 15,
    ""countComments"": 47,
    ""children"": {
        ""2"": {
            ""categoryID"": 191,
            ""name"": ""Watch"",
            ""description"": """",
            ""parentCategoryID"": 172,
            ""countDiscussions"": 1,
            ""countComments"": 0,
            ""children"": []
        }
    }
},
**{**
    **""categoryID"": 117,**
    **""name"": ""jewellery"",**
    **""description"": """",**
    **""parentCategoryID"": null,**
    **""countDiscussions"": 243,**
    **""countComments"": 6716,**
    **""children"": []**
**}**
    ]";

    var categories = JsonConvert.DeserializeObject<Category[]>(jsonData);


    foreach (var category in categories)
    {
        Console.WriteLine($"Category ID: {category.CategoryID}");
        Console.WriteLine($"Name: {category.Name}");
        Console.WriteLine($"Description: {category.Description}");
        Console.WriteLine($"Parent Category ID: {category.ParentCategoryID}");
        Console.WriteLine($"Count of Discussions: {category.CountDiscussions}");
        Console.WriteLine($"Count of Comments: {category.CountComments}");
        Console.WriteLine("Children:");
        foreach (var childCategory in category.Children)
        
        {
            var child = childCategory.Value.ToObject<ChildCategory>();
            Console.WriteLine($"    Category ID: {child.CategoryID}");
            Console.WriteLine($"    Name: {child.Name}");
            Console.WriteLine($"    Description: {child.Description}");
            Console.WriteLine($"    Parent Category ID: {child.ParentCategoryID}");
            Console.WriteLine($"    Count of Discussions: {child.CountDiscussions}");
            Console.WriteLine($"    Count of Comments: {child.CountComments}");
        }
    }
}
}

I’m able to load the data if I don’t include the last node (categoryID=117, highlighted with **). But if I include it then my code is failing. Could anyone help me to fix this?

2

Answers


  1. I think that one class should be enough

    var categories = JsonConvert.DeserializeObject<Category[]>(jsonData);
    
    public class Category
    {
        public string CategoryID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string ParentCategoryID { get; set; }
        public string CountDiscussions { get; set; }
        public string CountComments { get; set; }
        public Dictionary<int, Category> Children { get; set; }
        public Category[] ChildrenArray { get; set; }
    
        public Category(JToken Children)
        {
            if(Children.Type == JTokenType.Object) this.Children=Children.ToObject<Dictionary<int, Category>>();
            else ChildrenArray = Children.ToObject<Category[]>();
        }
    }
    

    or if you prefer JObject and JArray change it to JToken

    public class Category
    {
    //...another properties
    
    public JToken Children { get; set; }
    }
    
    Login or Signup to reply.
  2. Hi go to check google search key word "convert json string to c# class" lot of online class generator website was available

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search