2011-03-30 54 views
1

我试图在下面的问题结束时反序列化示例JSON。但是results.count始终为0 我怎么可以反序列化JSON这与多种数据,所以我可以用循环用JavaScriptSerializer反序列化多个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 
} 
}" 

回答

1

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

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

然后:

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

是不是可以用.net的内置库来实现? – nLL 2011-03-30 12:56:29

+0

@nLL,nope。 JavaScriptSerializer不支持这种JSON格式。你能得到的最好的是'serializer.DeserializeObject(json)',它返回非强类型对象的字典。 – 2011-03-30 12:57:46

+0

bugger :)我知道必须包括一个二进制只有几行! – nLL 2011-03-30 14:03:29

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

实际JSON验证正确 – nLL 2011-03-30 12:55:52

+0

你JSON字符串本身以及格式化JSON字符串。但是它不能被反序列化为Foo对象列表。如果你想反序列化到'List ',你应该使用JSON内容,我建议 – archil 2011-03-30 13:01:20