I want to convert my Dictionary<int,List<int>>
to JSON string. Does anyone know how to achieve this in C#?
相关问题
- Jackson Deserialization not calling deserialize on
- Sorting 3 numbers without branching [closed]
- How to maintain order of key-value in DataFrame sa
- Graphics.DrawImage() - Throws out of memory except
- StackExchange API - Deserialize Date in JSON Respo
This will write to the console:
Json.NET probably serializes C# dictionaries adequately now, but when the OP originally posted this question, many MVC developers may have been using the JavaScriptSerializer class because that was the default option out of the box.
If you're working on a legacy project (MVC 1 or MVC 2), and you can't use Json.NET, I recommend that you use a
List<KeyValuePair<K,V>>
instead of aDictionary<K,V>>
. The legacy JavaScriptSerializer class will serialize this type just fine, but it will have problems with a dictionary.Documentation: Serializing Collections with Json.NET
You could use JavaScriptSerializer.
This answer mentions Json.NET but stops short of telling you how you can use Json.NET to serialize a dictionary:
As opposed to JavaScriptSerializer,
myDictionary
does not have to be a dictionary of type<string, string>
for JsonConvert to work.This is Similar to what Meritt has posted earlier. just posting the complete code