3

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 ?

5
  • Have you tried public oodleListingsUser[] user; ? Commented Feb 7, 2012 at 9:37
  • @L.B Yes ! and It runs perfectly if jsonString returns array of user ! But if jsonString returns one user it will give No parameterless constructor defined for type of 'oodleListingsUser[]' error Commented Feb 7, 2012 at 9:38
  • And Even I have parameter less constructor in my class code ! Still it gives error ! Commented Feb 7, 2012 at 9:40
  • 1
    Try: public oodleListings[] listings = new oodleListings[0]; Or make it a List<oodleListings> perhaps. Commented Feb 7, 2012 at 10:53
  • @NoOne: I'll make an answer :) Glad it worked! Commented Feb 7, 2012 at 12:09

1 Answer 1

4

Try:

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

Or make it a List<oodleListings> perhaps.

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried List<oodleListings> and its working completely fine ! Thank you @leppie

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.