System.MissingMethodException:反序列化json数组时出错
本文关键字:数组 出错 json 反序列化 MissingMethodException System | 更新日期: 2023-09-27 18:22:44
反序列化jsonString时出错。
错误为Type 'oodleListingsUser' is not supported for deserialization of an array.
我的反序列化代码是
string jsonString = new WebClient().DownloadString("http://api.oodle.com/api/v2/listings?key=TEST®ion=region_value&category=category_value&format=json");
JavaScriptSerializer ser = new JavaScriptSerializer();
jsonOodleApi p = ser.Deserialize<jsonOodleApi>(jsonString);
我对jsonOodleApi的类定义是
public class jsonOodleApi
{
public oodleCurrent current;
public oodleListings[] listings;
public oodleMeta meta;
public string state { get; set; }
}
对oodleCurrent和oodleMeta的定义我不会放弃,因为它是完美的!
oodleListings的定义是
public class oodleListings
{
public string id { get; set; }
public string title { get; set; }
public oodleListingsUser user;
// I have skipped some of fields because it have no issue at all.
}
oodleListingsUser的定义是
public class oodleListingsUser
{
public string id { get; set; }
public string url { get; set; }
public string name { get; set; }
public string photo { get; set; }
}
问题是我的jsonString有时只返回一个user值(类型为oodleListingsUser),有时它返回user的数组,有时返回user的null!
当它只返回一个用户时,它运行得非常好!没有问题。
但当它返回用户数组时,bloom发生错误Type 'oodleListingsUser' is not supported for deserialization of an array.
就连我也试过public oodleListingsUser[] user
,但它给出了错误
No parameterless constructor defined for type of 'oodleListingsUser[]'
对于只返回一个用户的值!
现在我该怎么解决这个问题呢?
尝试:
public oodleListings[] listings = new oodleListings[0];
或者让它成为List<oodleListings>
。