使用JavaScriptSerializer反序列化多个JSON结果

时间:2011-03-30 12:10:26

标签: c# .net json serialization javascriptserializer

我正在尝试在下面的问题结尾处反序列化示例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 string:

"{ "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
}
}";

3 个答案:

答案 0 :(得分:1)

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

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

然后:

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

答案 1 :(得分:0)

什么是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
}]"

答案 2 :(得分:0)

我不同意见。 JavaScriptSerializer可以反序列化Dictionary<string, Foo>

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