0

first time asking questions here.

I currently have a Dictionary in C# that I want to serialize into JSON using:

Dictionary<string, List<string> myDict = new Dictionary<string, List<string>();
string json = JsonConvert.SerializeObject(myDict);

Works A1, getting results

{"Key1":["Item1","Item2","Item3"],"Key2":["Item1","Item2","Item3"]}

However, I would like to get my JSON response in this format to be usable with Jquery addons:

{"ID":"Key1", "Value": ["Val":"Item1","Val:":"Item2","Val":"Item3"] }, { "ID":"Key2", "Value":["Val":"Item1","Val:":"Item2","Val":"Item3"] }

Without having to loop through the dictionary and rebuild it manually. Is there any decent ways to do it?

Thank you

Gweek
  • 1
  • 1
  • 3
    The JSON format you're trying to achieve is invalid; you can't have an associative array as in your `Value` property and the objects would need to be in an array. Syntax issues aside, you would probably be best to create your own class with `ID` and `Value` properties and then add that to a `List<>` before serialising it to JSON – Rory McCrossan Aug 30 '16 at 15:20
  • Possible duplicate of [How do I convert a dictionary to a JSON String in C#?](http://stackoverflow.com/questions/5597349/how-do-i-convert-a-dictionary-to-a-json-string-in-c) – MethodMan Aug 30 '16 at 15:56
  • Seconding what @RoryMcCrossan said, if you upload your desired JSON to http://jsonlint.com/ you will get parse errors such as `Expecting 'EOF', '}', ',', ']', got ':'`. Json.NET isn't going to be able to directly generate a string in that format. – dbc Aug 30 '16 at 18:04

0 Answers0