8

First of all let me tell you one thing that I am posting this question is just for eagerness and to increase my knowledge. Hope you can clear my doubts !

Now lets come to the point

I got this question from my previous question-answer

Actually the problem is if I use

List<oodleListings> listings;

instead of

oodleListings[] listings;

It works fine !! I can deserialize my json string easily.

Now the problem is why array is not supported while deserializing json ?

What is the actual reason to use List instead of array ?

3 Answers 3

9

Your problem is not related with Arrays or Lists. See the example classes below

public class TestClassList
{
    public List<User> users;
}

public class TestClassArray
{
    public User[] users;
}

public class User
{
    public string user;
}

and assume your input string is string json1 = "{ users:[{user:'11'},{user:'22'}] }";

var obj1= ser.Deserialize<TestClassList>(json1);
var obj2 = ser.Deserialize<TestClassArray>(json1);

both deserializations will work..

But if you try to deserialize this string string json2 = "{ users:{user:'11'} }";

var obj3 = ser.Deserialize<TestClassList>(json2);
var obj4 = ser.Deserialize<TestClassArray>(json2);   //<--ERROR  

you will get error in the second line (Althoug first line doesn't give an error, it doesn't return a valid object either).

As a result: The second json string does not contain an array of users, this is why you get No parameterless constructor defined for type of 'User[]'.

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

1 Comment

What is ser in this context?
3

List<T> does not have a fixed length, so you are free to Add items to that object, without knowing its size. List is definitely the more flexible/functional class, but as a side-effect, it has a larger memory footprint.

Also, an object of type List<oodleListing> will have a method AddRange method that takes a parameter of oodleListing[] so you could always deserialize and then add to your generic class.

Comments

3

There is nothing wrong with Array or List. I just tried your code and it works without any issue for both Array and List.

In your previous question you didn't give the JSON serialized string, other wise it would have been solved there it self. You can check this post from ASP.Net.

2 Comments

I accept ! I have not provided JSON serialized string but I have given link ! That particular link returns json data !
api.oodle.com/api/v2/… Try this ! this will return value but different different categories returns diff values ! and it will be accessed by key! N due to company privacy I cant give Key ! Sorry ! But you can go further through developer.oodle.com

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.