How to convert the following JSON array into IDictionary<string, object>?

If you already have the JArray, all you have to do is convert it to a dictionary I guess.

Roughly something like this:

IDictionary<string,object> dict = jarray.ToDictionary(k=>((JObject)k).Properties().First().Name, v=> v.Values().First().Value<object>());

Check this for complete code with an example

I think there might be a better way to convert it to a dictionary though. I'll keep looking.


the JsonConvert.DeserializeObject<T> Method takes a JSON string, in other words a serialized object.
You have a deserialized object, so you'll have to serialize it first, which is actually pointless, considering you have all the information you need right there in the JArray object. If you are aiming just to get the objects from the array as key value pairs, you can do something like this:

Dictionary<string, object> myDictionary = new Dictionary<string, object>();

foreach (JObject content in jarray.Children<JObject>())
{
    foreach (JProperty prop in content.Properties())
    {
        myDictionary.Add(prop.Name, prop.Value);
    }
}

Tags:

C#

Json

Json.Net