无法将JSON对象数组反序列化为C#对象
问题描述:
我无法使JavascriptSerializer反序列化对象数组。我不是非常确定我错过了什么。无法将JSON对象数组反序列化为C#对象
这里是我的对象......
public class HomefinderResult
{
public Data data;
public List<Listing> listings;
public Status status;
public class Listing
{
public string recordingDate;
public Buyer buyer;
public double distance;
public string id;
public class Buyer
{
public Address address;
public string phone;
public string firstName;
public string lastName;
public class Address
{
public string line1;
public string city;
public string state;
public string zip;
public string county;
public string latitude;
public string longitude;
}
}
}
public class Data
{
public Meta meta;
public class Meta
{
public int totalMatched;
public int totalPages;
public int currentPage;
public double executionTime;
}
}
public class Status
{
public int code;
public string[] errorStack;
}
}
,这里是我试图反序列化JSON响应...
{
"data":{
"meta":{
"totalMatched":126,
"totalPages":7,
"currentPage":1,
"executionTime":0.26153302192688
},
"listings":[
{
"recordingDate":"2010-06-07",
"buyer":{
"address":{
"line1":"999 Meadow Rd",
"city":"Oak Ridge",
"state":"TN",
"zip":"99999",
"county":"Anderson",
"latitude":"99.0316920000",
"longitude":"-99.2476320000"
},
"phone":"9995551234",
"firstName":"xxx",
"lastName":"xxx"
},
"distance":18.088793566409,
"purchasePrice":"975000",
"id":"70a706d46c5e1db7417b78c159467431"
},
{
"recordingDate":"2010-06-04",
"buyer":{
"address":{
"line1":"999 Willow Ln",
"city":"Oak Ridge",
"state":"TN",
"zip":"99999",
"county":"Anderson",
"latitude":"99.0045700000",
"longitude":"-99.3182850000"
},
"firstName":"xxx",
"lastName":"xxx",
"gender":"Female"
},
"distance":22.244494041996,
"purchasePrice":"201000",
"id":"dda41ec8150c8648a7e9926b2c5fd468"
}
]
},
"status":{
"code":200,
"errorStack":null
}
}
我在做什么错?
答
你有3个类,但是只有一个包装器,所以你需要一个包装其他3的类来匹配JSON中的根类“级”级别。
答
切换到JSON.NET并有同样的问题后,我走开了几分钟,然后回来。我发现我的问题 - 我需要在列表中嵌入数据,而不是在同一级别。
你不应该发布实际数据:-p – Fredou 2010-09-02 19:22:34
它的公共记录:)尽管你是绝对正确的。 – 2010-09-03 19:58:44