Gordon Hickley Gordon Hickley - 11 months ago 55
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


How I can get this to:-

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

Preferably using Newtonsoft.Json or jQuery

Answer Source

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

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