【问题标题】:How do I convert a Dictionary<string,string> to a json string using System.Json?如何使用 System.Json 将 Dictionary<string,string> 转换为 json 字符串?
【发布时间】:2009-09-19 00:33:43
【问题描述】:

我正在尝试将 Silverlight 中的字典转换为字符串。我不想使用任何第三方库,所以想使用 System.Json 来执行此操作?

到目前为止我想到的最好的方法是将字典中的所有项目添加到 JsonObject 中,然后调用 toString(),任何更好的想法都会很有帮助。

【问题讨论】:

    标签: .net silverlight json


    【解决方案1】:
    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多