5

In a Windows Phone app I need to parse JSON data. I am able to get the values for keys which aren't nested. But if the JSON has arrays inside arrays, then how do I extract values from the JSON file?

In the past what I did was parse the JSON into a JArray object, then from the JToken I got the value of a specified string key.

In the JSON below, people has men and women, and men itself has many men with different IDs. So if I have this complete thing as a JSON string, how do I print the value of the ID of a particular man? I could have converted into JArray and access indexed values if there were no nested arrays here, but how to do it now?

Here is my JSON:

{
    "people": [
        {
            "men": [
                {
                    "id": 0,
                    "name": "alex",
                    "age": 25
                },
                {
                    "id": 1,
                    "name": "bob",
                    "age": 26
                },
                {
                    "id": 2,
                    "name": "charlie",
                    "age": 27
                }
            ]
        },
        {
            "women": [
                {
                    "id": 0,
                    "name": "alexys",
                    "age": 25
                },
                {
                    "id": 1,
                    "name": "bethany",
                    "age": 26
                },
                {
                    "id": 2,
                    "name": "catherine",
                    "age": 27
                }
            ]
        }
    ]
}
1
  • Are you using Newtonsoft.Json? There doesn't seem to be anything tricky here; without code, it's difficult to tell where you're going wrong. Commented Nov 18, 2014 at 11:00

3 Answers 3

14

From the top-level JToken, you can use SelectToken() to navigate to the JArray that has the data you are interested in:

JToken token = JToken.Parse(json);
JArray men = (JArray)token.SelectToken("people[0].men");

From there you can process the JArray as you normally would:

foreach (JToken m in men)
{
    Console.WriteLine("id: " + m["id"]);
    Console.WriteLine("name: " + m["name"]);
    Console.WriteLine("age: " + m["age"]);
    Console.WriteLine();
}

Same thing for the women array, except the SelectToken() path would be people[1].women.

DEMO: https://dotnetfiddle.net/7BoiUO

Sign up to request clarification or add additional context in comments.

2 Comments

This was the most helpful parsing technique I got :) Thanks mate
Same here. You are a savior! So far, this is the only way to "lazily" parse JSONs in C# that worked for me.
2

Use the http://json2csharp.com/ It generates the classes. I can't copy it, because you put here as a picture, not text. For the array you have to create other class. In your case you will have a People class that contains Men[] and Women[] arrays. The Men and Women classes contain an another array, which contains the Id, Name, Age. I develop a similar app, and I use the Newtonsoft Json. It works perfectly with the arrays as well.

1 Comment

Handy little site!
1

Use Json.net

You can add it via nuget. Here's a good guide to nested json parsing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.