【发布时间】: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