System.MissingMethodException: Error while deseria

2020-04-11 18:23发布

问题:

I am getting error while deserializing jsonString.

Error is Type 'oodleListingsUser' is not supported for deserialization of an array.

My code of deserialization is

    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);

My class defination of jsonOodleApi is

public class jsonOodleApi
{
    public oodleCurrent current;
    public oodleListings[] listings;
    public oodleMeta meta;

    public string state { get; set; }

}

Defination of oodleCurrent and oodleMeta I am not giving because its perfect !

Defination of oodleListings is

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.

}

Defination of oodleListingsUser is

public class oodleListingsUser
{
    public string id { get; set; }
    public string url { get; set; }
    public string name { get; set; }
    public string photo { get; set; }
}

The problem is my jsonString sometimes returns only one value of user (type of oodleListingsUser), and sometimes it returns array of user, and sometimes it returns null of user !

When it returns only one user, it runs perfectly fine ! No issue.

But when it returns array of user, bloom ! error occurs Type 'oodleListingsUser' is not supported for deserialization of an array.

Even I have tried public oodleListingsUser[] user But it gives error as

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

for the value which returns only one user !

Now what should i do to solve this issue ?

回答1:

Try:

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

Or make it a List<oodleListings> perhaps.