0

I'm wanting to run a foreach loop through a nested List in this JSON.

I'm getting some JSON that looks like:

{
    "name":"Placeholder",
    "entries":
        [
            {
                "playerId": "27271906",
                "playerName": "Billy"
            },
            {
                "playerId": "35568613",
                "playerName": "Jeeves"
            }
        ]
}

Classes:

public class IDs
{
    public string playerId { get; set; }
}

public class Top
{
    public List<IDs> entries { get; set; }
}

When I go to run the program, it seems to not work when it gets to:

List<string> pros = new List<string>();
using (var web = new WebClient())
{
    web.Encoding = System.Text.Encoding.UTF8;
    var jsonString = responseFromServer;
    var jss = new JavaScriptSerializer();
    var ProsList = jss.Deserialize<List<IDs>>(jsonString);
    int i = 1;

    foreach (IDs x in ProsList)
    {                        
        pros.Add(x.playerId);
        i++;
        if (i == 3)
        {
            break;
        }
    }
}

When I have it set up like this, it will say that I can't use a foreach since there's no enumerator. Any idea? I'm new to C# and this syntax, so it may be really easy for some people to see. Thanks!

I would add that I'm using Visual Studio 2013.

2
  • possible duplicate of Deserialize JSON into C# dynamic object? Commented Dec 5, 2014 at 7:34
  • I think it's more about the foreach enumeration, idk though. Commented Dec 5, 2014 at 11:59

1 Answer 1

1

You have to deserialize the json into Top instead of List<IDs>, and enumerate result.entries. Change your code to below

List<string> pros = new List<string>();
using (var web = new WebClient())
{
    web.Encoding = System.Text.Encoding.UTF8;
    var jsonString = responseFromServer;
    var jss = new JavaScriptSerializer();
    var result = jss.Deserialize<Top>(jsonString);
    int i = 1;

    foreach (IDs x in result.entries)
    {                        
        pros.Add(x.playerId);
        i++;
        if (i == 3)
        {
            break;
        }
    }
}

Alternatively you can also use JSON.NET like below

List<string> pros = new List<string>();
using (var web = new WebClient())
{
    web.Encoding = System.Text.Encoding.UTF8;
    var jsonString = responseFromServer;

    var result = JsonConvert.DeserializeObject<Top>(jsonString);
    int i = 1;

    foreach (IDs x in result.entries)
    {                        
        pros.Add(x.playerId);
        i++;
        if (i == 3)
        {
            break;
        }
    }
}

Working demo: https://dotnetfiddle.net/naqPx2

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

1 Comment

Thanks so much. I'm so dense sometimes. :) I'll check it off in 6 minutes

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.