0

I am a new user of Json.NET. I have created a type: Person that has different attributes: firstName, lastName, age, etc.

I have a Json string but I can't manage to deserialize it and get all the "Person" in a stored list.

Here is my piece of code I am running:

List <Person> persons = JsonConvert.DeserializeObject<List<Person>>(strPersons);` 

strPersons is my json string. It is like that:

string strPersons = @"{
    'Columns':['FirstName','LastName','Hobbies','Age','Country','Address','Phone','Gender'],
    'Rows':[ 
       ['X', 'Y', 'Cuisine', '35', 'France', 'unknown', 'unknown', 'male'],      
       ['W', 'Z', 'Danser', '43', 'France', 'unknown', 'unknown', 'male'],
        ...]

My error when compiling is:

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type System.Collections.Generic.List1[Info3JsonConvertor.Person]' because the type requires a JSON array

Can someone explain me how can I deserialize my json string to get a list Will I have the same problem if I serialize a List<Person> into json string?
I know those questions have already been answered but if someone can explain me with my example?

7
  • 2
    "My error when compiling" that doesn't sound like a compile-time error. Please provide a short but complete example demonstrating the problem. But note that your strPersons only defines a single object, with fields of Columns and Rows. It's not an array... Commented Aug 3, 2015 at 14:21
  • 1
    You should add in the details of your Person object that you are trying to deserialize it into. Chances are the c# object doesn't match your json object in some way. In fact you are trying to deserialize it into a list but as the error says your json object isn't an array, it is two properties, row and column. What data do you expect to actually come out of this json into your list person? Commented Aug 3, 2015 at 14:21
  • 2
    Pro tip: Copy the Json string to your clipboard, then in Visual Studio, Edit menu, Paste Special and select Paste JSON as Classes. That will give you an object you can deserialise into. Commented Aug 3, 2015 at 14:27
  • @DavidG: That is an awesome pro tip. Will have to remember that one next time I need to make classes for some json (which in fact will be soon). Commented Aug 3, 2015 at 14:30
  • @Chris yeah, i understood, i was expecting to get a List<Person> I thought json.net read my rows as new person Commented Aug 3, 2015 at 14:31

2 Answers 2

1

The object that the deserializer recognizes from your JSON looks something like that:

public class RootObject
{
    public List<string> Columns { get; set; }
    public List<List<string>> Rows { get; set; }
}

As you can see it's not List, but just Person

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

4 Comments

In fact I suspect that Rows is actually List<Person> based on the data in it and that the OP actually wants to just get Rows out as List<Person>.
If that's the case, than either there's need to add wrapper object, and than get just the rows or fix the JSON response to suit the expected data (if it's an option)
Yup. I mentioned it because if that is the case then it wouldn't take much change to your objet for it to be the wrapper object in question.
@FelixAv as mentionned, I want my Rows ( <=> List<Person>) to be deserialized in a List<Person>
0

Your root element is an object not a list or array ({....}). Json arrays start with [

public class MyData
{
    public List<string> Columns { set; get; }
    public List<List<string>> Rows { set; get; }
}

var data = JsonConvert.DeserializeObject<MyData>(json);

1 Comment

Thanks for answering. I must practise more to get it on my own next time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.