【发布时间】:2015-11-12 18:49:46
【问题描述】:
我正在尝试将 JSON/XML 字符串填充到 C# 对象中。 我将我的 XML 转换为 JSON,然后使用 JSON.NET。示例:
JSON 字符串
{
"persons":[
{
"age":30,
"name":"david",
"hobbies":[
{
"name":"tennis",
"hours":5
},
{
"name":"football",
"hours":10
}
]
},
{
"name":"adam",
"age":23,
"hobbies":[]
}
]
}
C# 类
public class Hobbies
{
public string name;
public int hours;
}
class Person
{
public string name;
public int age;
public List<Hobbies> hoobies = new List<Hobbies>();
}
我正在尝试将数据填充到人员列表中:
List<Person> persons = new List<Person>();
JsonConvert.PopulateObject(myJsonText, persons);
我得到了这个例外:
无法将 JSON 对象填充到类型上
我该怎么做?
【问题讨论】:
标签: c# json xml json.net converter