How to convert a dictionary <string, string> to json string using System.Json?

I am trying to convert a dictionary in Silverlight to string. I don't want to use third party libraries, so I would like to use System.Json for this?

The best way I've thought of so far is to add all the items in the dictionary to a JsonObject and then call toString (), any better ideas would be most helpful.

+2


source to share


1 answer


Dictionary<string, string> d = ...;

JsonObject jo = new JsonObject(
    from kv in d
    select new KeyValuePair<string, JsonValue>(
        kv.Key,
        new JsonPrimitive(kv.Value)));
string json = jo.ToString();

      



+3


source







All Articles