简体   繁体   English

C#修复此Json反序列化类型错误?

[英]C# fix this Json deserialization type error?

I have a Json file in the following format: 我有以下格式的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"]}

and I am using the following code to put it into a list: 我正在使用以下代码将其放入列表中:

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 is telling me that the "Adorable Kitten" part of the Json can not be deserialized. Visual Studio告诉我Json的“可爱的小猫”部分不能反序列化。 Normally I would get rid of that portion of the code but it is an excerpt from a file that is nearly 40000 lines long, so removing that for each item would be impractical. 通常,我会摆脱代码的这一部分,但这是一个文件的摘录,该文件的长度接近40000行,因此为每个项目删除该代码都是不切实际的。 Additionally when i removed "Adorable Kitten" while troubleshooting I got a similar error for "layout". 另外,当我在排除故障时删除“可爱的小猫”时,也遇到了类似的“布局”错误。 The error says that i need to either put it into a Json array or change the deserialized Type so that it is a normal .Net Type. 该错误表明我需要将其放入Json数组或更改反序列化类型,以便它是常规的.Net类型。 Can anyone point what I'm doing wrong? 谁能指出我做错了什么?

If your example is really what you are doing then you're simply deserializing to the wrong type. 如果您的示例确实是您在做什么,那么您只是在反序列化为错误的类型。

Right now your code would work for the following: 现在,您的代码适用于以下情况:

[{"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"]}]

Notice that it is a single JSON object inside a JSON array. 请注意,它是JSON数组内的单个JSON对象。 This corresponds to the type you are deserializing to ( List<Item> ). 这对应于您要反序列化为( List<Item> )的类型。

The example you posted of your JSON file isn't valid JSON (unless there are curly braces around the whole thing you left out) so you need to fix the file. 您发布的JSON文件示例不是有效的JSON(除非您遗漏的整个内容周围都有花括号),因此您需要修复该文件。 If you really want there to be a list of Items in the JSON then wrapping everything in a single array will be the correct way to represent that. 如果您确实希望在JSON中有一个Items列表,那么将所有内容包装在一个数组中将是正确的表示方式。

First check if that the JSON you're receiving is a valid JSON, apparently the one you're receiving is wrong, you can check in https://jsonlint.com 首先检查您收到的JSON是否为有效JSON,显然您收到的JSON是错误的,您可以在https://jsonlint.com中检入

Second create a model for the JSON, you can do it here http://json2csharp.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;
    }
}

Don't forget about the getters and setters on your model. 不要忘记模型上的吸气剂和吸气剂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM