[英]C# deserialize json with array
我想將包含數組的 json 反序列化為 class,但我不知道它是如何工作的。 我正在使用 C# WPF。 到目前為止我要做的工作:
Class:
class Truppe
{
public int id { get; set; }
public int nr { get; set; }
public int tribe { get; set; }
}
Json 反序列化:
Troop = "{\"id\":1,\"nr\":1,\"tribe\":1}";
Truppe JSONObj = JsonConvert.DeserializeObject<Truppe>(Troop);
到目前為止,它工作得很好。 但現在我想要 class 中的一個數組,其中包含作為數組的成本。 所以它應該看起來像那樣(不工作)
Class:
class Truppe
{
public int id { get; set; }
public int nr { get; set; }
public int tribe { get; set; }
public int[,] costs { get; set; }
}
Json 反序列化:
Troop = "{\"id\":1,\"nr\":1,\"tribe\":1,\"costs\":{\"1\":75,\"2\":50,\"3\":100,\"4\":0}}";
Truppe JSONObj = JsonConvert.DeserializeObject<Truppe>(Troop);
有人可以幫我得到正確的表格嗎?
好的,我發現它如何與 Json 的語法一起使用。 代替
public int[,] costs { get; set; }
我不得不使用
public Dictionary<string, int> costs { get; set; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.