希望有人能发现我做错了什么。 CardCollection.cs仅包含一个名为cards的列表,具有一个用作getter / setter的公共方法Cards。
我应该指出,我正在使用Visual Studio 2017进行开发,当我使用内置的JSON Visualizer在调试模式下检查字符串“ jsonString”时,它将正确显示在该窗口中。不幸的是,JsonConvert.DeserializeObject没有正确设置我的CardCollection对象,并导致卡片列表为空
来自“ Form.cs”
private async void SelectFileButton_ClickAsync(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "JSON Files|*.json";
dialog.Title = "Select a JSON File";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string line = "";
string jsonString = "";
string fileName = dialog.FileName;
StringBuilder builder = new StringBuilder("");
using (StreamReader reader = File.OpenText(dialog.FileName))
{
while ((line = await reader.ReadLineAsync()) != null)
{
line = line.Replace("\t", "");
line = line.Replace("\n", "");
builder.Append(line);
}
}
jsonString = builder.ToString();
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
CardCollection cardList = JsonConvert.DeserializeObject<CardCollection>(jsonString, settings);
}
}
Card.cs
public class Card
{
string[] colorIdentity;
string[] colors;
double convertedManaCost;
ForeignData[] foreignData;
bool isReserved;
string layout;
Legality[] legalities;
string loyalty;
string manaCost;
string name;
string power;
string[] printings;
Ruling[] rulings;
string[] subtypes;
string[] supertypes;
string text;
string toughness;
string type;
string[] types;
string uuid;
public string[] ColorIdentity { get => colorIdentity; set => colorIdentity = value; }
public string[] Colors { get => colors; set => colors = value; }
public double ConvertedManaCost { get => convertedManaCost; set => convertedManaCost = value; }
public bool IsReserved { get => isReserved; set => isReserved = value; }
public string Layout { get => layout; set => layout = value; }
public string Loyalty { get => loyalty; set => loyalty = value; }
public string ManaCost { get => manaCost; set => manaCost = value; }
public string Name { get => name; set => name = value; }
public string Power { get => power; set => power = value; }
public string[] Printings { get => printings; set => printings = value; }
public string[] Subtypes { get => subtypes; set => subtypes = value; }
public string[] Supertypes { get => supertypes; set => supertypes = value; }
public string Text { get => text; set => text = value; }
public string Toughness { get => toughness; set => toughness = value; }
public string Type { get => type; set => type = value; }
public string[] Types { get => types; set => types = value; }
public string Uuid { get => uuid; set => uuid = value; }
internal ForeignData[] ForeignData { get => foreignData; set => foreignData = value; }
internal Legality[] Legalities { get => legalities; set => legalities = value; }
internal Ruling[] Rulings { get => rulings; set => rulings = value; }
}
{
"1999 World Championships Ad": {
"colorIdentity": [],
"colors": [],
"convertedManaCost": 0.0,
"foreignData": [],
"isReserved": false,
"layout": "token",
"legalities": {},
"name": "1999 World Championships Ad",
"printings": [
"WC99"
],
"rulings": [],
"starter": true,
"subtypes": [],
"supertypes": [],
"text": "",
"type": "Card",
"types": [
"Card"
],
"uuid": "8635dea0-985a-4c02-a02b-8b08d96fb2dd"
},
"2004 World Championships Ad": {
"colorIdentity": [],
"colors": [],
"convertedManaCost": 0.0,
"foreignData": [],
"isReserved": false,
"layout": "token",
"legalities": {},
"name": "2004 World Championships Ad",
"printings": [
"WC04"
],
"rulings": [],
"starter": true,
"subtypes": [],
"supertypes": [],
"text": "",
"type": "Card",
"types": [
"Card"
],
"uuid": "cbd6a762-f3c2-4b29-a07e-f0c9c81cf1ec"
},
"A Display of My Dark Power": {
"colorIdentity": [],
"colors": [],
"convertedManaCost": 0.0,
"foreignData": [],
"isReserved": false,
"layout": "scheme",
"legalities": {},
"name": "A Display of My Dark Power",
"printings": [
"ARC"
],
"rulings": [
{
"date": "2010-06-15",
"text": "The ability affects all players, not just you."
},
{
"date": "2010-06-15",
"text": "The effect doesn’t wear off until just before your next untap step (even if an effect will cause that untap step to be skipped)."
},
{
"date": "2010-06-15",
"text": "The types of mana are white, blue, black, red, green, and colorless."
},
{
"date": "2010-06-15",
"text": "If a land produces more than one type of mana at a single time (as Boros Garrison does, for example), the land’s controller chooses which one of those types of mana is produced by the delayed triggered ability."
},
{
"date": "2010-06-15",
"text": "If a land is tapped for mana but doesn’t produce any (for example, if you tap Gaea’s Cradle for mana while you control no creatures), the delayed triggered ability won’t trigger."
}
],
"starter": true,
"subtypes": [],
"supertypes": [],
"text": "When you set this scheme in motion, until your next turn, whenever a player taps a land for mana, that player adds one mana of any type that land produced.",
"type": "Scheme",
"types": [
"Scheme"
],
"uuid": "c5287e77-890d-41bd-acc6-7a8f1866426d"
}
}
答案 0 :(得分:2)
"1999 World Championships Ad": {
这里有一个对象,而不是数组。您应该将其反序列化为字典:
JsonConvert.DeserializeObject<Dictionary<string, Card>>(...);
答案 1 :(得分:1)
您的类<Card style={{ flex: 1 }}>
在几种方面与您的json不匹配。有几种好的方法可以创建直接对json进行序列化/反序列化的类。
方法1:
这将创建反序列化数据所需的类。
方法2: 使用Quicktype.io。粘贴您的json并以您喜欢的语言获取所需的类。
类Card
的另一个问题是属性名称区分大小写! ColorIdentity!= colorIdentity。
如果要在代码中保留PascalCasing,但在Json中有camelCasing,则需要这样装饰每个属性:
Card
此外,您的json中的竞赛名称是您的课程中不存在的对象。如果您使用方法1或2,您会明白我的意思。