【问题标题】:C# fix this Json deserialization type error?C#修复这个Json反序列化类型错误?
【发布时间】:2018-03-22 00:34:58
【问题描述】:

我有一个格式如下的 Json 文件:

  "Adorable Kitten": {"layout": "normal","name": "Adorable Kitten","manaCost": "{W}","cmc": 1,"colors": ["White"],"type": "Host Creature — Cat","types": ["Host","Creature"],"subtypes": ["Cat"],"text": "When this creature enters the battlefield, roll a six-sided die. You gain life equal to the result.","power": "1","toughness": "1","imageName": "adorable kitten","colorIdentity": ["W"]}

我正在使用以下代码将其放入列表中:

using (StreamReader r = new StreamReader(filepath))
                {
                    string json = r.ReadToEnd();
                    List<Item> items = JsonConvert.DeserializeObject<List<Item>>(json);
                    textBox1.Text = items[0].name.ToString();
                }
public class Item
        {
            public string layout;
            public string name;
            public string manaCost;
            public string cmc;
            public string[] colors;
            public string type;
            public string[] types;
            public string[] subtypes;
            public string text;
            public string power;
            public string toughness;
            public string imageName;
            public string[] colorIdentity;
        }

Visual Studio 告诉我 Json 的“可爱小猫”部分无法反序列化。通常我会删除那部分代码,但它是一个近 40000 行长的文件的摘录,因此为每个项目删除它是不切实际的。此外,当我在故障排除时删除“可爱的小猫”时,“布局”出现了类似的错误。该错误表明我需要将其放入 Json 数组或更改反序列化类型,使其成为正常的 .Net 类型。谁能指出我做错了什么?

【问题讨论】:

  • 你的json数据格式错误。

标签: c# json json.net json-deserialization


【解决方案1】:

如果您的示例确实是您正在做的,那么您只是在反序列化为错误的类型。

现在您的代码适用于以下情况:

[{"layout": "normal","name": "Adorable Kitten","manaCost": "{W}","cmc": 1,"colors": ["White"],"type": "Host Creature — Cat","types": ["Host","Creature"],"subtypes": ["Cat"],"text": "When this creature enters the battlefield, roll a six-sided die. You gain life equal to the result.","power": "1","toughness": "1","imageName": "adorable kitten","colorIdentity": ["W"]}]

请注意,它是 JSON 数组中的单个 JSON 对象。这对应于您要反序列化的类型 (List&lt;Item&gt;)。

您发布的 JSON 文件示例不是有效的 JSON(除非您遗漏的整个内容都带有花括号),因此您需要修复该文件。如果您真的希望在 JSON 中有一个项目列表,那么将所有内容包装在一个数组中将是表示它的正确方法。

【讨论】:

    【解决方案2】:

    首先检查你收到的JSON是不是有效的JSON,显然你收到的是错误的,你可以检查https://jsonlint.com

    第二次为JSON创建模型,你可以在这里http://json2csharp.com

    public class AdorableKitten
    {
        public string layout { get; set; }
        public string name { get; set; }
        public string manaCost { get; set; }
        public int cmc { get; set; }
        public List<string> colors { get; set; }
        public string type { get; set; }
        public List<string> types { get; set; }
        public List<string> subtypes { get; set; }
        public string text { get; set; }
        public string power { get; set; }
        public string toughness { get; set; }
        public string imageName { get; set; }
        public List<string> colorIdentity { get; set;
        }
    }
    

    不要忘记模型上的 getter 和 setter。

    【讨论】:

    • 这对我不起作用,因为“可爱的小猫”只是数千个不同 Json 对象中的一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多