Rickard B.
Rickard B.

Reputation: 564

Parse JSON data with JSON.NET

There are a lot of questions about this out there, but I couldn't find a solution to my problem.

I have JSON that looks like this:

{
"index":[
  {
     "Color":"Blue",
     "URL":"SomeURL",

     "Persons":[
        {
           "name":"Charlie",
           "Country":"Denmark",
           "Security number":"25663456"
        }
     ],

     "Color":"Green",
     "URL":"SomeURL",

     "Persons":[
        {
           "name":"Putin",
           "Country":"Russia",
           "Security number":"78495832"
         }
       ],
    ],
  } 
 "total":"2"
}

The only JSON data I can access is index and total.

How do I access and print out ONLY name, Country or Color?

Upvotes: 1

Views: 116

Answers (2)

Ryan
Ryan

Reputation: 1368

The index is an array of objects. To access it, you'll have to loop over it, or access each element by its index in the array. Then, you'll have access to the properties you have set on it in the feed.

You can do something like this if you're using the JSON.Net lib:

dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(target)
foreach(var item in jsonObj.index)
{
    string color = item.Color;
}

Upvotes: 1

Peter
Peter

Reputation: 27934

index is an array. index[0].Color will give you "Blue" etc...

Upvotes: 2

Related Questions