System.MissingMethodException:反序列化json数组时出错

时间:2012-02-07 09:30:19

标签: c# asp.net json json.net deserialization

反序列化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&region=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 有时只返回一个用户值( oodleListingsUser 的类型),有时会返回用户数组,有时会返回用户的null !

当它只返回一个用户时,它运行得非常好!没问题。

但当它返回用户数组时,会出现 bloom!错误 Type 'oodleListingsUser' is not supported for deserialization of an array.

即使我已经尝试public oodleListingsUser[] user但是它会出错

No parameterless constructor defined for type of 'oodleListingsUser[]'

表示只返回一个用户的值!

现在该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:4)

尝试:

public oodleListings[] listings = new oodleListings[0]; 

或者也许是List<oodleListings>