gpt4 book ai didi

c# - 将 JSON 反序列化为对象。有些属性总是相同的,有些则不同

转载 作者:行者123 更新时间:2023-11-30 21:38:20 25 4
gpt4 key购买 nike

我正在尝试构建一个用户可以上传 json 文件的函数。

json 文件中的每一行都可以有不同的属性(即列)。其中 5 个属性始终相同,因此我希望将它们反序列化为一个对象。其余的属性必须进入字典或其他东西。

这是一个 json 示例:

[{
"Projekt": "Bakker Bouw Service",
"Ruimte": "Hoofdgebouw",
"Apparaat": {
"project": "Bosboom001",
"versie": "812"
},
"Apparaat naam": "",
"Status": "Goedgekeurd",
"Testname1": "",
"Testname3": "2000-01-04T10:37:00+01:00",
"Testname7": "2001-01-03T00:00:00+01:00"
}, {
"Projekt": "Bakker Bouw Service",
"Ruimte": "Hoofdgebouw",
"Apparaat": {
"project": "Vlaams003",
"versie": "713"
},
"Apparaat naam": "",
"Status": "Goedgekeurd",
"Testname1": "Slecht",
"Testname7": "2000-01-04T10:37:00+01:00",
"Testname9": "2001-01-03T00:00:00+01:00",
"Testname16": "18MOhm",
"Testname23": "OK"
}, {
"Projekt": "Bakker Bouw Service",
"Ruimte": "Hoofdgebouw",
"Apparaat": {
"project": "Vlaams017",
"versie": "73"
},
"Apparaat naam": "GDR34Z5",
"Status": "Afgekeurd",
"Testname7": "2000-01-04T10:37:00+01:00",
"Testname10": "0,012mA",
"Testname16": "200MOhm",
"Testname23": "200MOhm",
"Testname25": "Afgekeurd",
"Testname31": "0,01mA"
}
]

这是要反序列化的类:

public class KeuringRegel
{
public string Projekt { get; set; }
public string Ruimte { get; set; }
public Apparaat Apparaat { get; set; }
[JsonProperty(PropertyName = "Apparaat naam")]
public string Apparaatnaam { get; set; }
public string Status { get; set; }
public Dictionary<string, object> testNames { get; set; }
}

public class Apparaat
{
public string project { get; set; }
public string versie { get; set; }
}

这是 Controller

public IActionResult Upload(IFormFile file)
{
string fileContent = null;
using (var reader = new StreamReader(file.OpenReadStream()))
{
fileContent = reader.ReadToEnd();
}
List<KeuringRegel> keuringRegelList = JsonConvert.DeserializeObject<List<KeuringRegel>>(fileContent);
//More stuff here
}

json 成功反序列化,但 testNames 值始终为 null。我明白为什么,因为 Json 文件中没有 testNames 属性。但是,我如何实现我想要的?我不是 Json 专家。

最佳答案

假设有只有 testNameNNNN 条目作为补充值,您可以这样做的一种方法是使用 JsonExtensionDataAttribute像这样:

public class KeuringRegel
{
public string Projekt { get; set; }
public string Ruimte { get; set; }
public Apparaat Apparaat { get; set; }
[JsonProperty(PropertyName = "Apparaat naam")]
public string Apparaatnaam { get; set; }
public string Status { get; set; }
[JsonExtensionData()]
public Dictionary<string, object> testNames { get; set; }
}

这将为您提供不属于其他属性之一的值:

enter image description here

它有点“钝器”,但您始终可以对返回的 KeuringRegel 实例执行一些后处理,以从 testNames 中删除任何错误的条目(即与模式 testNameNNNN)。

如果您的 JSON 确实包含与模式 testNameNNNN 不匹配的内容,因此会包含在内,您可以为 testNames< 实现自定义类属性:

public class KeuringRegel
{
public string Projekt { get; set; }
public string Ruimte { get; set; }
public Apparaat Apparaat { get; set; }
[JsonProperty(PropertyName = "Apparaat naam")]
public string Apparaatnaam { get; set; }
public string Status { get; set; }
[JsonExtensionData()]
public TestNames testNames { get; set; }
}

public class TestNames : Dictionary<string, object>
{
public new void Add(string key, object value)
{
if (key.StartsWith("testname", StringComparison.OrdinalIgnoreCase))
{
base.Add(key, value);
}
}
}

这将检查添加到 testNames 字典中的每个项目并阻止其添加,如果(如在我的评论中,我在 "badgerBadgetCatCat"的 JSON 中有一个项目:3) 它与模式不匹配。

关于c# - 将 JSON 反序列化为对象。有些属性总是相同的,有些则不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46037945/

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