0

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?

1
  • 1
    Your json data is a wrong format. Commented Mar 22, 2018 at 0:48

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

this wont work for me as the "Adorable Kitten" is just 1 of several thousand different Json objects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.