C#使用数组反序列化JSON

问题描述 投票:0回答:1

我想反序列化包含一个数组的json到一个类中,我不知道它是如何工作的。我正在使用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 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);

有人可以帮助我获取正确的表格吗?

c# json json.net data-conversion
1个回答
0
投票

好吧,我发现它与Json的语法一起工作。代替

public int[,] costs { get; set; }

我不得不使用

public Dictionary<string, int> costs { get; set; }
© www.soinside.com 2019 - 2024. All rights reserved.