John John - 2 months ago 7
C# Question

Loop Json Properties in C#

I receive the following JSON string. It is part of the specifications of products. The data represents the Header ("General") and the items within in Key value pairs.

I dont want to hard code any properties such as general, Display Features and should be able to


  1. Retrieve the Header

  2. Iterate on the key value pair.



So Far I have the following code

foreach (var SpecList in objAPIProduct.categorySpecificInfoV1.specificationList)
{
foreach(dynamic SpecItem in SpecList.General)
{
Console.WriteLine(SpecItem.key);
Console.WriteLine(SpecItem.value[0]);
}
}


I want to get rid of the "General" Property that I have hardcoded in the inner foreach loop and make the loop generic for all properties.
Here is the Json that I have

{
"General": [{
"key": "Sales Package",
"value": ["Laptop, Power Adapter, Warranty Document"]
},
{
"key": "Model Number",
"value": ["3558"]
},
{
"key": "Part Number",
"value": ["Z565103UIN9"]
},
{
"key": "Model Name",
"value": ["Inspiron 15"]
},
{
"key": "Series",
"value": ["Inspiron"]
},
{
"key": "Color",
"value": ["Black"]
},
{
"key": "Type",
"value": ["Notebook"]
},
{
"key": "Suitable For",
"value": ["Everyday Use"]
},
{
"key": "Battery Backup",
"value": ["Upto 3.5 hours"]
},
{
"key": "Battery Cell",
"value": ["4 cell"]
}]
},
{
"Processor and Memory Features": [{
"key": "Processor Brand",
"value": ["Intel"]
},
{
"key": "Processor Name",
"value": ["Core i3"]
},
{
"key": "Graphic Processor",
"value": ["Intel HD Graphics 5500"]
},
{
"key": "SSD",
"value": ["No"]
},
{
"key": "RAM",
"value": ["4 GB"]
},
{
"key": "RAM Type",
"value": ["DDR3"]
},
{
"key": "HDD Capacity",
"value": ["500 GB"]
},
{
"key": "Processor Variant",
"value": ["5005U"]
},
{
"key": "Clock Speed",
"value": ["2 GHz"]
},
{
"key": "Memory Slots",
"value": ["1 Slot"]
},
{
"key": "Expandable Memory",
"value": ["Upto 4 GB"]
},
{
"key": "RAM Frequency",
"value": ["1600 MHz"]
},
{
"key": "Cache",
"value": ["3 MB"]
},
{
"key": "RPM",
"value": ["5400"]
}]
}

Answer

After trying out a lot of options, I found that it is not possible to get the "Name" of the property when we use Json.Net or JavascriptSerialiser.

Finally, I used the help of this thread -

Deserialize JSON into C# dynamic object?

wherein the person has suggested using System.Web.Helpers and it works perfect in my case.

Here is my final code: dynamic objAPIProduct= Json.Decode(json);

List<ProductSpecification> objProductSpecificationList = new List<ProductSpecification>();
            ProductSpecification objProductSpecification;

            foreach (var SpecListCategoryList in objAPIProduct.categorySpecificInfoV1.specificationList)
            {
               foreach (var SpecCategory in SpecListCategoryList)
                {

                    DynamicJsonArray objListitems = SpecCategory.Value;

                    for  (int  i = 0; i < objListitems.Length; i++)
                     {
                    objProductSpecification = new ProductSpecification();
                    objProductSpecification.SpecificationHead = SpecCategory.Key;
                    objProductSpecification.SpecificationKey = objListitems[i].key;
                    objProductSpecification.SpecificationValue = objListitems[i].value[0];
                    objProductSpecificationList.Add(objProductSpecification);
                     }
                }

            }
Comments