How to deserialize json to C# class with arbitrary number of values and attributes

88 views Asked by At

I'm trying to consume some JSON files, which could contains a arbitrary number of child values.

By simplifying the JSON, I get something like:

{
"timestamp":1710415627927,
"parent":{
  "id":"680867",
  "key":"XXX",
  "childs":{
     "customchild_1":{
        "value":"DIS",
        "id":"16805"
     },
    "customchild_2":null,
    "customchild_3":null,
    "customchild_4":[
        "01 (CHIL-172)"
     ],
    "customchild_5":[
        "02 (CHIL-44553)"
     ],
    "customchild_6":{
        "value":"Achievement",
        "id":"13425",
     }
    }
   }
}

Of course, each file could have different number of customfield_xxx attributes, and values associated can be (generally) above 3 kind of values (id / value, [] or null)

I try to create a first class for parent

public class parent
{
   public Dictionary<object, List<customchild>> customchilds { get; set; }
...
}

public class customchild
{
    public string value { get; set; }
    public string id { get; set; }
}

problem is that I don't know if it will work for all and on my main process, how I can :

  • count the number of customchild attributes in my JSON
  • access the Nth customchild ?
1

There are 1 answers

0
shahid abbas On

Utilize the Newtonsoft.Json Nuget Package to deserialize JSON as a parent object. From there, you can access the count of custom child attributes using parentObject.ParentDetails.Customchilds.Count.

It's functioning smoothly in my compiler.

 using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    public class Parent
    {
        [JsonProperty("timestamp")]
        public long Timestamp { get; set; }
    
        [JsonProperty("parent")]
        public ParentDetails ParentDetails { get; set; }
    }
    
    public class ParentDetails
    {
        [JsonProperty("id")]
        public string Id { get; set; }
    
        [JsonProperty("key")]
        public string Key { get; set; }
    
        [JsonProperty("childs")]
        public Dictionary<string, object> Customchilds { get; set; }
    }
    
    public class Customchild
    {
        public string Value { get; set; }
        public string Id { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"{
                ""timestamp"": 1710415627927,
                ""parent"": {
                    ""id"": ""680867"",
                    ""key"": ""XXX"",
                    ""childs"": {
                        ""customchild_1"": {
                            ""value"": ""DIS"",
                            ""id"": ""16805""
                        },
                        ""customchild_2"": null,
                        ""customchild_3"": null,
                        ""customchild_4"": [
                            ""01 (CHIL-172)""
                        ],
                        ""customchild_5"": [
                            ""02 (CHIL-44553)""
                        ],
                        ""customchild_6"": {
                            ""value"": ""Achievement"",
                            ""id"": ""13425""
                        },
                        
                    }
                }
            }";
    
            // Deserialize JSON
            Parent parentObject = JsonConvert.DeserializeObject<Parent>(json);
    
            // Count the number of customchild attributes
            int numberOfCustomchilds = parentObject.ParentDetails.Customchilds.Count;
      }
    }