Надеясь, что кто-то поймает, что я делаю неправильно.CardCollection.cs просто содержит список, называемый карточками, с открытыми методами Cards, которые действуют как получатель / установщик.
Я должен указать, что я использую Visual Studio 2017 для разработки, и когда я проверяю строку "jsonString" в режиме отладки с помощью встроенного JSON Visualizer, он правильно отображается в этом окне.К сожалению, 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; }
}
Вот пример файла JSON
{
"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"
}
}