Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用JavaScriptSerializer反序列化多个JSON结果_C#_.net_Json_Serialization_Javascriptserializer - Fatal编程技术网

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

C# 使用JavaScriptSerializer反序列化多个JSON结果,c#,.net,json,serialization,javascriptserializer,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 stri

我试图在这个问题的末尾用下面的代码反序列化示例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);
foreach(结果中的Foo r)
{
Response.Write(r.html);
}
[可序列化()]
公开课Foo
{
公共字符串html{get;set;}
公共字符串bannerid{get;set;}
公共字符串contenttype{get;set;}
公共字符串alt{get;set;}
公共字符串宽度{get;set;}
公共字符串高度{get;set;}
公共字符串url{get;set;}
公共字符串bannertext{get;set;}
公共字符串bannerurl{get;set;}
公共字符串活动ID{get;set;}
公共字符串版本{get;set;}
公共字符串cpc{get;set;}
}
公共列表GetFooMultiple(字符串PublisherRorSiteId)
{
JavaScriptSerializer ser=新的JavaScriptSerializer();
列表结果=序列反序列化(json);
返回结果;
}
列表结果=GetFooMultiple(“1111122222”);
响应.写入(结果.计数);
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
}
}"
“{”11111:{”alt:“无”,
“bannerid”:“21655”,
“bannertext”:“示例1”,
“横幅”:http://foo.com/foo.png",
“活动ID”:“1216”,
“内容类型”:“png”,
“cpc”:“0.00060”,
“高度”:50,
“html”:“,
“url”:”http://foo.com/foo.png",
“版本”:“1”,
“宽度”:320
},
“22222”:{“alt”:”,
“bannerid”:“21937”,
“bannertext”:“示例2”,
“横幅”:“,
“活动ID”:“1241”,
“contenttype”:“txt”,
“cpc”:“0.00060”,
“高度”:0,
“html”:“,
“url”:”http://foo.com/foo.png",
“版本”:“1”,
“宽度”: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
}
}";
string json=@“{”11111”:{
“alt”:“无”,
“bannerid”:“21655”,
“banner文本”:“示例1”,
“横幅”:http://foo.com/foo.png"",
“活动ID”:“1216”,
“内容类型”:“png”,
“cpc”:“0.00060”,
“高度”:50,
“html”“:”“”,
“url”:”http://foo.com/foo.png"",
“版本”:“1”,
“宽度”:320
},
""22222"" : {
“alt”:“alt”,
“bannerid”:“21937”,
“banner文本”:“示例2”,
“bannerurl”:”,
“活动ID”:“1241”,
“内容类型”:“txt”,
“cpc”:“0.00060”,
“高度”:0,
“html”“:”“”,
“url”:”http://foo.com/foo.png"",
“版本”:“1”,
“宽度”:0
}
}";

发布者站点ID的参数是什么,json字符串中的“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
}]"
“[{”alt:“无”,
“bannerid”:“21655”,
“bannertext”:“示例1”,
“横幅”:http://foo.com/foo.png",
“活动ID”:“1216”,
“内容类型”:“png”,
“cpc”:“0.00060”,
“高度”:50,
“html”:“,
“url”:”http://foo.com/foo.png",
“版本”:“1”,
“宽度”:320
},
{“alt”:“,
“bannerid”:“21937”,
“bannertext”:“示例2”,
“横幅”:“,
“活动ID”:“1241”,
“contenttype”:“txt”,
“cpc”:“0.00060”,
“高度”:0,
“html”:“,
“url”:”http://foo.com/foo.png",
“版本”:“1”,
“宽度”:0
}]"

发布者站点ID的参数是什么,以及“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
}]"
“[{”alt:“无”,
“bannerid”:“21655”,
“bannertext”:“示例1”,
“横幅”:http://foo.com/foo.png",
“活动ID”:“1216”,
“内容类型”:“png”,
“cpc”:“0.00060”,
“高度”:50,
“html”:“,
“url”:”http://foo.com/foo.png",
“版本”:“1”,
“宽度”:320
},
{“alt”:“,
“bannerid”:“21937”,
“bannertext”:“示例2”,
“横幅”:“,
“活动ID”:“1241”,
“contenttype”:“txt”,
“cpc”:“0.00060”,
“高度”:0,
“html”:“,
“url”:”http://foo.com/foo.png",
“版本”:“1”,
“宽度”:0
}]"
支持这种类型的结构。所以你可以这样做:

var result = JsonConvert.DeserializeObject<IDictionary<string, Foo>>(json);
支持这种类型的结构。所以你可以这样做:

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

我不敢苟同
JavaScriptSerializer
可以反序列化
字典

var results=ser.Deserialize(json);//json现在定义有问题
var foo1=结果[“11111”];
Console.WriteLine(结果[“11111”].bannerid);//预计为21655
输出

21655


我不敢苟同
JavaScriptSerializer
可以反序列化
字典

var results=ser.Deserialize(json);//json现在定义有问题
var foo1=结果[“11111”];
Console.WriteLine(结果[“11111”].bannerid);//预计为21655
输出

21655


使用.net的内置库难道不可能吗?@nLL,不可能。JavaScriptSerializer不支持这种JSON格式。你能得到的最好的结果就是
序列化程序。反序列化对象(json)
返回非强类型对象的字典。bugger:)我知道只需几行就必须包含一个二进制文件!使用.net的内置库难道不可能吗?@nLL,不可能。JavaScriptSerializer不支持这种JSON格式。你能得到的最好的结果就是
序列化程序。反序列化对象(json)
返回非强类型对象的字典。bugger:)我知道只需几行就必须包含一个二进制文件!JSON字符串本身就是格式良好的JSON字符串。但它不能反序列化到Foo对象列表中。如果您想反序列化到
列表
,应该使用JSON内容i advicedyou JSON字符串本身就是格式良好的JSON字符串。但它不能反序列化到Foo对象列表中。如果要反序列化到
列表中
,应该使用我建议的JSON内容