在使用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类型(例如,不是像整数这样的原始类型,也不是集合)类型(如数组或列表),可以从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);
}
}
希望对大家有所帮助。
我认为,使用您当前的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; }
}
}