neoniles
neoniles

Reputation: 41

Iterate json childnode c#

I want to iterate through the following json but I only able to display the items in the 1st node. Can anyone suggest how I could loop my JSON nodes?

Json :

string Json = @"{   ""@context"":""http://schema.org"",
        ""@type"":""ItemList"",
""url"":""https://www.mudah.my/Penang/ps3-for-sale"",
""numberOfItems"":""40"",
""itemListElement"":[      
{         ""@type"":""Product"",
     ""url"":""https://www.mudah.my"",
     ""name"":""ps4"",
     ""offers"":{
        ""@type"":""Offer"",
        ""priceCurrency"":""MYR"",
        ""price"":""1200""
},
     ""itemCondition"":""Second-hand (Used)"",
     ""category"":""Games-and-Consoles""
},
  {         ""@type"":""Product"",
     ""url"":""https://www.mudah.my"",
     ""name"":""ps3"",
     ""offers"":{
        ""@type"":""Offer"",
        ""priceCurrency"":""MYR"",
        ""price"":""500""
},
     ""itemCondition"":""Second-hand (Used)"",
     ""category"":""Games-and-Consoles""
}
]
}";

My code so far :

        dynamic json = JsonConvert.DeserializeObject(Json);


        string type = json.itemListElement[0].@type;
        string url = json.itemListElement[0].url;
        string name = json.itemListElement[0].name;
        string offer_type = json.itemListElement[0].offers.@type;
        string currency = json.itemListElement[0].offers.priceCurrency;
        string price = json.itemListElement[0].offers.price;
        string itemCondition = json.itemListElement[0].itemCondition;
        string category = json.itemListElement[0].category;

        Console.WriteLine(name);
        Console.WriteLine(url);
        Console.WriteLine(price);
        Console.WriteLine(itemCondition);


//Current Output only displaying items in 1st node : 
//ps4
//https://www.mudah.my
//1200
//Second-hand (Used)

How do I modify the code so it looping each node in 'itemListElement'?

Upvotes: 0

Views: 57

Answers (1)

NIKER
NIKER

Reputation: 474

You need to iterate like so:

dynamic json = JsonConvert.DeserializeObject(Json);
if (json == null)
{
    return;
}

for (int i = 0; i < json.itemListElement.Count; i++)
{
    string type = json.itemListElement[i].@type;
    string url = json.itemListElement[i].url;
    string name = json.itemListElement[i].name;
    string offer_type = json.itemListElement[i].offers.@type;
    string currency = json.itemListElement[i].offers.priceCurrency;
    string price = json.itemListElement[i].offers.price;
    string itemCondition = json.itemListElement[i].itemCondition;
    string category = json.itemListElement[i].category;

    Console.WriteLine(name);
    Console.WriteLine(url);
    Console.WriteLine(price);
    Console.WriteLine(itemCondition);
                
}

Better yet, this also works:

foreach (var item in json.itemListElement)
{
    string type = item.type;
    string url = item.url;
    string name = item.name;
    string offer_type = item.offers.type;
    string currency = item.offers.priceCurrency;
    string price = item.offers.price;
    string itemCondition = item.itemCondition;
    string category = item.category;

    Console.WriteLine(name);
    Console.WriteLine(url);
    Console.WriteLine(price);
    Console.WriteLine(itemCondition);
}

Upvotes: 2

Related Questions