简体   繁体   English

C# 用数组反序列化 json

[英]C# deserialize json with array

I want to deserialize a json that contains an array into a class and i dont know how it works.我想将包含数组的 json 反序列化为 class,但我不知道它是如何工作的。 I am using C# WPF.我正在使用 C# WPF。 What i got to work so far:到目前为止我要做的工作:

Class: Class:

class Truppe
{
    public int id { get; set; }
    public int nr { get; set; }
    public int tribe { get; set; }
}

Json Deserialize: Json 反序列化:

Troop = "{\"id\":1,\"nr\":1,\"tribe\":1}";
Truppe JSONObj = JsonConvert.DeserializeObject<Truppe>(Troop);

so far it works totally fine.到目前为止,它工作得很好。 But now i want an array in the class containing the costs as an array.但现在我想要 class 中的一个数组,其中包含作为数组的成本。 So it shall look something like that (not working)所以它应该看起来像那样(不工作)

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 Deserialize: Json 反序列化:

Troop = "{\"id\":1,\"nr\":1,\"tribe\":1,\"costs\":{\"1\":75,\"2\":50,\"3\":100,\"4\":0}}";
Truppe JSONObj = JsonConvert.DeserializeObject<Truppe>(Troop);

can someone help me get the correct form?有人可以帮我得到正确的表格吗?

Ok, i found out how it works with that Syntax of Json.好的,我发现它如何与 Json 的语法一起使用。 Instead of代替

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

i had to use我不得不使用

public Dictionary<string, int> costs { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM