使用JSON.NET进行反序列化时,我遇到以下JSON问题。
{
"?xml": {
"@version": "1.0",
"@encoding": "utf-8"
},
"Persons": {
"Person": [{
"@Id": "1",
"@Name": "John",
"@Surname": "Smith"
},
{
"@Id": "2",
"@Name": "John",
"@Surname": "Smith",
"Skills": {
"Skill": [{
"@Id": "1",
"@Name": "Developer"
},
{
"@Id": "2",
"@Name": "Tester"
}]
}
}]
}
}
我正在使用以下课程:
public class RootObject
{
public Xml xml { get; set; }
public Persons Persons { get; set; }
}
public class Xml
{
public string version { get; set; }
public string encoding { get; set; }
}
public class Persons
{
public List<Person> Person { get; set; }
}
public class Skill
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Skills
{
public List<Skill> Skill { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public Skills Skills { get; set; }
}
当我尝试反序列化时
RootObject persons = JsonConvert.DeserializeObject<RootObject>(json);
我收到以下错误:
无法反序列化当前的JSON对象(例如{“name”:“value”}) 进入类型 'System.Collections.Generic.List`1 [Project.Models.Persons.Skill]' 因为该类型需要一个JSON数组(例如[1,2,3])来反序列化 正确。要修复此错误,请将JSON更改为JSON数组 (例如[1,2,3])或更改反序列化类型以使其正常 .NET类型(例如,不是整数的基本类型,不是集合 类似于数组或List的类型,可以从JSON反序列化 宾语。 JsonObjectAttribute也可以添加到类型中以强制它 从JSON对象反序列化。
我认为问题在于符号:
"Skills": {
"Skill": [{
我缺少什么,这个问题有一个简单的解决方案吗?
更新
所以最后问题是它有时候是一个JSON数组
"Skills": {
"Skill": [{
有时是JSON对象
"Skills": {
"Skill": {
但是当我将代码粘贴/检查到验证器时,它总是被格式化为JSON数组,因此我使用监视窗口检查它以查看原始json字符串。
从那里可以很容易地用JsonConverter属性标记属性
public class Skills
{
[JsonConverter(typeof(MyConverter))]
public List<Skill> Skill { get; set; }
}
并编写转换器:
public class MyConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
return serializer.Deserialize<List<Skill>>(reader);
}
else
{
Skill skill = serializer.Deserialize<Skill>(reader);
return new List<Skill>(new[] { skill});
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value);
}
}
希望它对某人有帮助。
答案 0 :(得分:4)
我认为,使用您当前的JSON,您说明Skill
包含一个集合,而不是Skills
。请尝试使用此JSON:
"Skills": [
{
"@Id": "1",
"@Name": "Developer"
},
{
"@Id": "2",
"@Name": "Tester"
}
]
同样的事情适用于您如何定义Persons
集合。
修改强>
此测试通过我:
[TestFixture]
public class JSONTester
{
[Test]
public void Json_deserialize()
{
var json = @"{
""?xml"": {
""@version"": ""1.0"",
""@encoding"": ""utf-8""
},
""Persons"": {
""Person"": [{
""@Id"": ""1"",
""@Name"": ""John"",
""@Surname"": ""Smith""
},
{
""@Id"": ""2"",
""@Name"": ""John"",
""@Surname"": ""Smith"",
""Skills"": {
""Skill"": [{
""@Id"": ""1"",
""@Name"": ""Developer""
},
{
""@Id"": ""2"",
""@Name"": ""Tester""
}]
}
}]
}
}";
var persons = JsonConvert.DeserializeObject<RootObject>(json);
Assert.AreEqual(persons.Persons.Person[1].Skills.Skill.Count, 2);
}
public class RootObject
{
public Xml xml { get; set; }
public Persons Persons { get; set; }
}
public class Xml
{
public string version { get; set; }
public string encoding { get; set; }
}
public class Persons
{
public List<Person> Person { get; set; }
}
public class Skill
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Skills
{
public List<Skill> Skill { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public Skills Skills { get; set; }
}
}