Gordon Hickley Gordon Hickley - 1 year ago 127
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));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download