【发布时间】:2021-06-15 20:14:50
【问题描述】:
我有一个 JSON 文件 people.json 包含多个对象,我想将这些对象解析为包含 Person 对象但没有运气的列表 People。我从 peopleLis 得到 null ,我怀疑我在这里做错了什么(可能很多)。你们能帮帮我吗?
{
"Andrew": {
"weight": "75",
"height": "181"
},
"Nathalie": {
"weight": "68",
"height": "182"
},
"Dave": {
"weight": "83",
"height": "192"
}
}
这是我试过的代码:
public class Person
{
public int weight { get; set; }
public int height { get; set; }
}
public class People
{
public List<Person> people { get; set; }
}
static void Main(string[] args)
{
People peopleList = JsonConvert.DeserializeObject<People>(File.ReadAllText(@"C:\people.json"));
}
【问题讨论】:
-
作为问题所在的线索,您可以将 JSON 放入 this page,然后查看生成的类。
-
json 应该以 [ 开头并以 ] 结尾,这是一个对象列表。目前你有 {, }..