【问题标题】:Deserializing multiple JSON result with JavaScriptSerializer使用 JavaScriptSerializer 反序列化多个 JSON 结果
【发布时间】:2011-03-30 12:10:26
【问题描述】:

我正在尝试在此问题的末尾反序列化示例 JSON,如下所示。但 results.count 始终为 0。 我怎样才能用多个数据反序列化这个 JSON,以便我可以循环使用

foreach (Foo r in results)
{
   Response.Write(r.html);
}

[Serializable()]
public class Foo
{
public string html { get; set; }
public string bannerid { get; set; }
public string contenttype { get; set; }
public string alt { get; set; }
public string width { get; set; }
public string height { get; set; }
public string url { get; set; }
public string bannertext { get; set; }
public string bannerurl { get; set; }
public string campaignid { get; set; }
public string version { get; set; }
public string cpc { get; set; }
}

public List<Foo> GetFooMultiple(string publisherOrSiteId)
{
        JavaScriptSerializer ser = new JavaScriptSerializer();
        List<Foo> results = ser.Deserialize<List<Foo>>(json);
        return results;        
}

List<Foo> results = GetFooMultiple("11111,22222");
Response.Write(results.Count);

Json 字符串:

"{ "11111" : { "alt" : "none",
  "bannerid" : "21655",
  "bannertext" : "Sample 1",
  "bannerurl" : "http://foo.com/foo.png",
  "campaignid" : "1216",
  "contenttype" : "png",
  "cpc" : "0.00060",
  "height" : 50,
  "html" : "<a href='http://foo.com/foo.png'><img src='http://foo.com/foo.png' width='320' height='50' alt='foo' /></a>",
  "url" : "http://foo.com/foo.png",
  "version" : "1",
  "width" : 320
},
  "22222" : { "alt" : "",
  "bannerid" : "21937",
  "bannertext" : "Sample 2",
  "bannerurl" : "",
  "campaignid" : "1241",
  "contenttype" : "txt",
  "cpc" : "0.00060",
  "height" : 0,
  "html" : "<a href='http://foo.com/foo.pngD'>Sample 2</a>",
  "url" : "http://foo.com/foo.png",
  "version" : "1",
  "width" : 0
}
}"

JSON 作为 C# 字符串,用于 MVCE:

    string json = @"{ ""11111"" : {
        ""alt"" : ""none"",
  ""bannerid"" : ""21655"",
  ""bannertext"" : ""Sample 1"",
  ""bannerurl"" : ""http://foo.com/foo.png"",
  ""campaignid"" : ""1216"",
  ""contenttype"" : ""png"",
  ""cpc"" : ""0.00060"",
  ""height"" : 50,
  ""html"" : ""<a href='http://foo.com/foo.png'><img src='http://foo.com/foo.png' width='320' height='50' alt='foo' /></a>"",
        ""url"" : ""http://foo.com/foo.png"",
        ""version"" : ""1"",
  ""width"" : 320
},
  ""22222"" : {
        ""alt"" : """",
  ""bannerid"" : ""21937"",
  ""bannertext"" : ""Sample 2"",
  ""bannerurl"" : """",
  ""campaignid"" : ""1241"",
  ""contenttype"" : ""txt"",
  ""cpc"" : ""0.00060"",
  ""height"" : 0,
  ""html"" : ""<a href='http://foo.com/foo.pngD'>Sample 2</a>"",
  ""url"" : ""http://foo.com/foo.png"",
  ""version"" : ""1"",
  ""width"" : 0
}
}";

【问题讨论】:

    标签: c# .net json serialization javascriptserializer


    【解决方案1】:

    JSON.NET 支持这种类型的结构。所以你可以这样做:

    var result = JsonConvert.DeserializeObject<IDictionary<string, Foo>>(json);
    

    然后:

    var foo1 = result["11111"];
    var foo2 = result["22222"];
    

    【讨论】:

    • 难道不能用 .net 的内置库来实现吗?
    • @nLL,不。 JavaScriptSerializer 不支持这种 JSON 格式。你能得到的最好的结果是serializer.DeserializeObject(json),它返回非强类型对象的字典。
    • bugger :) 我知道必须只包含几行的二进制文件!
    【解决方案2】:

    publisherOrSiteId 参数是什么,“11111”和“22222”在您的 json 字符串中做什么?正确的 JSON 应该是

    "[{ "alt" : "none",
      "bannerid" : "21655",
      "bannertext" : "Sample 1",
      "bannerurl" : "http://foo.com/foo.png",
      "campaignid" : "1216",
      "contenttype" : "png",
      "cpc" : "0.00060",
      "height" : 50,
      "html" : "<a href='"http://foo.com/foo.png'><img src='"http://foo.com/foo.png' width='320' height='50' alt='foo' /></a>",
      "url" : ""http://foo.com/foo.png",
      "version" : "1",
      "width" : 320
    },
       { "alt" : "",
      "bannerid" : "21937",
      "bannertext" : "Sample 2",
      "bannerurl" : "",
      "campaignid" : "1241",
      "contenttype" : "txt",
      "cpc" : "0.00060",
      "height" : 0,
      "html" : "<a href='http://foo.com/foo.pngD'>Sample 2</a>",
      "url" : "http://foo.com/foo.png",
      "version" : "1",
      "width" : 0
    }]"
    

    【讨论】:

    • 你的 JSON 字符串本身就是格式正确的 JSON 字符串。但它不能反序列化为 Foo 对象列表。如果你想反序列化成List&lt;Foo&gt;,你应该使用我建议的 JSON 内容
    【解决方案3】:

    我不敢苟同。 JavaScriptSerializer 可以反序列化一个Dictionary&lt;string, Foo&gt;

    var results = ser.Deserialize<Dictionary<string, Foo>>(json); // json now defined in question
    var foo1 = results["11111"];
    Console.WriteLine(results["11111"].bannerid); // 21655 expected
    

    输出

    21655

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      相关资源
      最近更新 更多