简体   繁体   中英

C# fix this Json deserialization type error?

I have a Json file in the following format:

  "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. 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. 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. 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. This corresponds to the type you are deserializing to ( 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. 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.

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

Second create a model for the JSON, you can do it here 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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