Geesu Geesu -4 years ago 226
C# Question

How do I enumerate through a JObject?

I'm trying to determine how to access the data that is in my JObject and I can't for the life of me determine how to use it.

JObject Object = (JObject)Response.Data["my_key"];


I can print it to the console doing Console.WriteLine(Object) and I see the data, it looks like:

{
"my_data" : "more of my string data"
...
}


But I have NO idea how to just iterate/enumerate through it, anyone have any ideas? I'm at such a loss right now.

Answer Source

If you look at the documentation for JObject, you will see that it implements IEnumerable<KeyValuePair<string, JToken>>. So, you can iterate over it simply using a foreach:

foreach (var x in obj)
{
    string name = x.Key;
    JToken value = x.Value;
    …
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download