1

我正在尝试在这个问题的末尾反序列化示例 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
}
}";
4

3 回答 3

1

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

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

进而:

var foo1 = result["11111"];
var foo2 = result["22222"];
于 2011-03-30T12:30:56.323 回答
0

json 字符串中的参数是什么publisherOrSiteId,“11111”和“22222”在做什么?正确的 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
}]"
于 2011-03-30T12:26:39.750 回答
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

于 2018-03-12T19:16:25.680 回答