[英]JsonConvert.DeserializeObject<T> Returns null list
希望有人能抓住我在做錯的事情。 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"
}
}
"1999 World Championships Ad": {
您在這里擁有的是一個對象,而不是一個數組。 您應該將其反序列化為字典:
JsonConvert.DeserializeObject<Dictionary<string, Card>>(...);
您的課程Card
在幾種方面與json不匹配。 有幾種好的方法可以創建直接對JSON進行序列化/反序列化的類。
方法1:
這將創建您需要反序列化數據的類。
方法2:使用Quicktype.io 。 粘貼您的json並以您喜歡的語言獲取所需的類。
類Card
另一個問題是屬性名稱區分大小寫! ColorIdentity!= colorIdentity。
如果要在代碼中保留PascalCasing,但在Json中有camelCasing,則需要這樣裝飾每個屬性:
[JsonProperty("colorIdentity")]
public object[] ColorIdentity { get; set; }
同樣,您的json中的競賽名稱也是您的課程中不存在的對象。 如果您遵循方法1或2,您將明白我的意思。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.