Gordon Hickley Gordon Hickley - 3 months ago 9
JSON Question

JSON convert dictionary to a list of key value pairs

I have a Dictionary <string, string>

If I Json.Encode this I get

{"Apple":"Apples","Orange":"Oranges"}


How I can get this to:-

[{ value: "Apples", key: "Apple" }, { value: "Oranges", key: "Orange"}]


Preferably using Newtonsoft.Json or jQuery

Answer

Convert it to a list of key value pairs before passing to the JSON serializer:

JsonConvert.SerializeObject(new List<KeyValuePair<string,string>>(dictionary));