Reputation: 468
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?
Upvotes: 0
Views: 2284
Reputation: 15364
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);
Upvotes: 0
Reputation: 1254
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
Upvotes: 1