gpt4 book ai didi

c# - JSON 映射到 C# 类

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:09 27 4
gpt4 key购买 nike

我有一段这样的 JSON:

{
"Site 1": [
{
"Stuff": [
"detergent",
"det1"
],
"productLine": "Hay",
"revenue": {
"1": 3123,
"2": 123123,
"3": 123123
}
},
{
"Stuff": [
"detergent",
"det2"
],
"productLine": "Machine",
"revenue": {
"1": 343123,
"2": 1231321323,
"3": 321
}
}
],
"Site 2": [
{
"Stuff": [
"detergent",
"det1"
],
"productLine": "Hay",
"revenue": {
"1": 111,
"2": 123123,
"3": 3213
}
},
{
"Stuff": [
"detergent",
"det2"
],
"productLine": "Machine",
"revenue": {
"1": 11123,
"2": 3255,
"3": 6575
}
}
]
}

我想将其映射到 C# 类,但我的问题是名称 Site1site2 是我认为应该是类名的名称吗?有没有办法将其表示为 C# 对象?

最佳答案

您的根对象实际上是一个 Dictionary<string, List<YourClass>>YourClass看起来像这样:

public class YourClass
{
public string[] Stuff { get; set; }
public string productLine { get; set; }
public Dictionary<string, int> revenue { get; set; }
}

现在您可以像这样反序列化您的 json:

var dic = JsonConvert.DeserializeObject<Dictionary<string, List<YourClass>>>("Your json string goes here...");

关于c# - JSON 映射到 C# 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31958362/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com