c# – 反序列化JSON不能使用JSON.NET

使用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);

我收到以下错误:

Cannot deserialize the current JSON object (e.g. {“name”:”value”})
into type
‘System.Collections.Generic.List`1[Project.Models.Persons.Skill]’
because the type requires a JSON array (e.g. [1,2,3]) to deserialize
correctly. To fix this error either change the JSON to a JSON array
(e.g. [1,2,3]) or change the deserialized type so that it is a normal
.NET type (e.g. not a primitive type like integer, not a collection
type like an array or List) that can be deserialized from a JSON
object. JsonObjectAttribute can also be added to the type to force it
to deserialize from a JSON object.

我想问题在于符号:

"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,您将描述技能包含集合,而不是技能.请尝试使用此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; }
        }
    }

转载注明原文:c# – 反序列化JSON不能使用JSON.NET - 代码日志