有人可以帮助我以正确的方式以这种格式反序列化 json:
[
{person: {name: "James", age:26}},
{person: {name: "Mary", age:36}},
{person: {name: "Kofi", age:46}}
]
我正在使用的代码如下:
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);
}
我映射到的类如下:
public class person
{
public string name{get;set;}
public int age{get; set;}
}
public class response
{
public person person {get;set;}
}
运行此代码时,我不断收到 name 和 age 属性的 null 和空字符串。如果有人可以帮助我,我将不胜感激。