Question:
How to efficiently convert Dictionary<int, List<int>>
to JSON string in C#?
Answer:
An effective way to accomplish this task is to use the functionality of Json.NET. With Json.NET, the conversion process becomes very simple:
<code class="language-csharp">string json = JsonConvert.SerializeObject(myDictionary);</code>
It is worth noting that unlike JavaScriptSerializer
, Json.NET does not require the dictionary to conform to the <string, string>
type constraints. This makes handling various dictionary types more flexible.
The above is the detailed content of How to Efficiently Convert a Dictionary to a JSON String in C#?. For more information, please follow other related articles on the PHP Chinese website!