Iterating over JSON object in C#

165,968

Solution 1

dynamic dynJson = JsonConvert.DeserializeObject(json);
foreach (var item in dynJson)
{
    Console.WriteLine("{0} {1} {2} {3}\n", item.id, item.displayName, 
        item.slug, item.imageUrl);
}

or

var list = JsonConvert.DeserializeObject<List<MyItem>>(json);

public class MyItem
{
    public string id;
    public string displayName;
    public string name;
    public string slug;
    public string imageUrl;
}

Solution 2

You can use the JsonTextReader to read the JSON and iterate over the tokens:

using (var reader = new JsonTextReader(new StringReader(jsonText)))
{
    while (reader.Read())
    {
        Console.WriteLine("{0} - {1} - {2}", 
                          reader.TokenType, reader.ValueType, reader.Value);
    }
}
Share:
165,968
require_once
Author by

require_once

Updated on July 05, 2022

Comments

  • require_once
    require_once almost 2 years

    I am using JSON.NET in C# to parse a response from the Klout API. My response is like this:

    [
      {
        "id": "5241585099662481339",
        "displayName": "Music",
        "name": "music",
        "slug": "music",
        "imageUrl": "http://kcdn3.klout.com/static/images/music-1333561300502.png"
      },
      {
        "id": "6953585193220490118",
        "displayName": "Celebrities",
        "name": "celebrities",
        "slug": "celebrities",
        "imageUrl": "http://kcdn3.klout.com/static/images/topics/celebrities_b32741b6703151cc7bd85fba24c44c52.png"
      },
      {
        "id": "5757029936226020304",
        "displayName": "Entertainment",
        "name": "entertainment",
        "slug": "entertainment",
        "imageUrl": "http://kcdn3.klout.com/static/images/topics/Entertainment_7002e5d2316e85a2ff004fafa017ff44.png"
      },
      {
        "id": "3718",
        "displayName": "Saturday Night Live",
        "name": "saturday night live",
        "slug": "saturday-night-live",
        "imageUrl": "http://kcdn3.klout.com/static/images/icons/generic-topic.png"
      },
      {
        "id": "8113008320053776960",
        "displayName": "Hollywood",
        "name": "hollywood",
        "slug": "hollywood",
        "imageUrl": "http://kcdn3.klout.com/static/images/topics/hollywood_9eccd1f7f83f067cb9aa2b491cd461f3.png"
      }
    ]
    

    As you see, it contains 5 id tags. Maybe next time it would be 6 or 1 or some other number. I want to iterate over the JSON and get the value of each id tag. I can't run a loop without knowing how many there will be. How can I solve this?

  • require_once
    require_once almost 12 years
    It says missing reference to Micrsofot.CSHARp and System.Core.I added reference to both.It's APS.NET App
  • SHEKHAR SHETE
    SHEKHAR SHETE almost 10 years
    Unfortunately 'dynamic' keyword is available from framework 4.0 onwards :(
  • david.barkhuizen
    david.barkhuizen over 7 years
    although it is not a requirement, this is the only answer that allows you to iterate through a JObject without knowing the structure beforehand
  • Zoran
    Zoran over 6 years
    This is a very good answer, it gives the option to get a full control over parsed JSON. Very useful, thanks!
  • Daniel Silva
    Daniel Silva over 5 years
    Great!! First solution works like a charm in .NET Core 2.1
  • Christophe Chenel
    Christophe Chenel over 5 years
    @SHEKHARSHETE you must add the reference Microsoft.CSHAP to your project by clicking right on your project/Add/References../ then select Microsoft.CSHARP