I have following JSON string:
[
{ "Person" : { "Name" : "John", "Gender" : "male" } },
{ "Person" : { "Name" : "John", "Gender" : "male" } }
]
(As you may notice unfortunately I have a sort of "root" element for each object in the array. Without this "root" element the task becomes quite trivial.)
I have to deserialize it into a list of Person
class:
class Person {
public string Name { get; set; }
public string Gender { get; set; }
}
...
List<Person> ListPersons() {
return JsonConvert.DeserializeObject<List<Person>>(jsonString);
}
Is it possible to do with Json.NET without creating wrapper class like PersonResult?
class PersonResult {
public Person Person { get; set; }
}
...
List<Person> ListPersons() {
return JsonConvert.DeserializeObject<List<PersonResult>>(jsonString)
.Select(p => p.Person)
.ToList();
}
The perfect solution for me is to be able somehow explicitly specify this "root" (e.g. via attribute) and do not create any wrappers, helpers, etc.