0

I've put together the following code from this answer:

JArray jsonArray = JArray.Parse(json);
foreach(JObject jsonObject in jsonArray.Children<JObject>())
{
    foreach(JProperty jProperty in jsonObject.Properties())
    {
        int id = jProperty.id;
        string name = (string)jProperty.Name;
        textBox1.AppendText(id.ToString() + " // " + name + Environment.NewLine);
    }
}

A sample of the JSON I'm trying to parse is as following:

[{"id":"219","name":"Jimmy"},{"id":"220","name":"Bobby"},{"id":"218","name":"Arthur"}]

The answer I referenced deals with parsing key => value pairs, how can I parse an associative array?

1
  • Its just an array of key value pairs. Key: Id, Value 219 (or "219"). You could deserialize to get at the data more easily especially since you appear to be consuming all of it Commented Oct 5, 2018 at 20:00

1 Answer 1

2

JSON doesn't have "associative arrays". It has arrays and objects.

What you have here is an array of objects. So, JArray.Parse will give you a JArray, and each item in it is itself a JObject:

var array = JArray.Parse(json);
foreach(JObject obj in array)
{
    int id = obj.Value<int>("id");
    string name = obj.Value<string>("name");
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! I'm still really new to C# so these kind of things I need to learn. Really appreciate your response.

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.