JSON to Dictionary

Am looking to convert a JSON string (as follows) to a dictionary. [{'Key':'superuser','Value':'s'}]

Ideally, I would like to convert it a way that Dictionary[0] will be [superuser]=s.

But I end up having two elements in dictionary instead of one. Could anyone guide me ?

Current Code

 JsonConvert.DeserializeObject<List<Dictionary<string,string>>>(json)

Thanks

Jon Skeet
people
quotationmark

You're currently deserializing it as a list of dictionaries, when you only actually want a single dictionary.

There may be a cleaner way of doing this, but you can deserialize it as a list of key-value pairs, then convert that into a dictionary. Sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

public class Test
{
    static void Main()
    {
        string json = "[{'Key':'x','Value':'y'},{'Key':'a','Value':'b'}]";

        var dictionary = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(json)
            .ToDictionary(pair => pair.Key, pair => pair.Value);

        Console.WriteLine(dictionary["x"]); // y
        Console.WriteLine(dictionary["a"]); // b
    }
}

people

See more on this question at Stackoverflow