Thursday, April 21, 2011

How do I convert a Dictionary<string,string> to a json string using System.Json?

Hi,

I'm trying to convert a Dictionary in Silverlight to a string. I don't want to use any third-party libraries so would like to use System.Json to do this?

The best way I have 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.

From stackoverflow
  • 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();
    

0 comments:

Post a Comment