Can somebody help me with the proper way to deserialize json in this format:
[
{person: {name: "James", age:26}},
{person: {name: "Mary", age:36}},
{person: {name: "Kofi", age:46}}
]
The code I’m using is below:
WebRequest request = WebRequest.Create("url");
WebResponse response = request.GetResponse();
string json;
using (var sr = new StreamReader(response.GetResponseStream()))
{
json = sr.ReadToEnd();
}
var serializer = new JavaScriptSerializer();
var persons= serializer.Deserialize<List<response>>(json);
foreach (var item in persons)
{
Console.Write("name:" + item.name + " and age: " + item.age);
}
The class I’m mapping to is below:
public class person
{
public string name{get;set;}
public int age{get; set;}
}
public class response
{
public person person {get;set;}
}
I keep getting nulls and empty strings for the name and age properties when I run this code. I would appreciate it a lot if someone could help me out.
I had to make call item.person.name and item.person.age in the foreach loop and make sure all properties in the person class were of type string.