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

发布于 2024-10-28 05:07:01 字数 2747 浏览 6 评论 0原文

我试图在这个问题的末尾用下面的方法反序列化示例 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2024-11-04 05:07:01

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

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

然后:

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

JSON.NET supports this type of structures. So you could do this:

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

and then:

var foo1 = result["11111"];
var foo2 = result["22222"];
凉栀 2024-11-04 05:07:01

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
}]"

What is publisherOrSiteId parameter for and what are "11111" and "22222" doing in your json string? Correct JSON would be

"[{ "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
}]"
稀香 2024-11-04 05:07:01

我不敢苟同。 JavaScriptSerializer 可以反序列化 Dictionary

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

I beg to differ. JavaScriptSerializer can deserialize a 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

Output

21655

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文