使用 JavaScriptSerializer 反序列化多个 JSON 结果
我试图在这个问题的末尾用下面的方法反序列化示例 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
}
}"
The JSON as a C# string, for 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
}
}";
I'm trying to deserialize sample JSON at the end of this question with below. But results.count is always 0.
how can i deserialize this JSON with multiple data so i can loop with
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
}
}"
The JSON as a C# string, for 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
}
}";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JSON.NET 支持这种类型的结构。所以你可以这样做:
然后:
JSON.NET supports this type of structures. So you could do this:
and then:
publisherOrSiteId
参数的作用是什么?“11111”和“22222”在 json 字符串中做什么?正确的 JSON 是What is
publisherOrSiteId
parameter for and what are "11111" and "22222" doing in your json string? Correct JSON would be我不敢苟同。
JavaScriptSerializer
可以反序列化Dictionary
:输出
I beg to differ.
JavaScriptSerializer
can deserialize aDictionary<string, Foo>
:Output