gpt4 book ai didi

c# - 如何将具有动态根名称的 json 转换为 C# 列表

转载 作者:行者123 更新时间:2023-12-02 01:27:40 24 4
gpt4 key购买 nike

var stringResult = await HttpHelper.PostAsync(batchUrl, content);

调用上面的行后,我在上面的 stringResult 变量中得到以下结果作为 C# 中的 API 响应。我想将下面的“Items”值转换为 C# 列表。在每个结果中,第一个 GUID 都是不同的。如何将以下结果转换为 C# 列表。我只想要元素。

{
"0934009e-6518-4440-91e3-c252d2e2cc4f": {
"Status": 200,
"Headers": {
"Content-Type": "application/json; charset=utf-8"
},
"Content": {
"Items": [{
"Timestamp": "2021-10-18T14:00:00Z",
"Value": 20.7,
"UnitsAbbreviation": "ppm"
}, {
"Timestamp": "2021-10-25T14:00:00Z",
"Value": 15.9,
"UnitsAbbreviation": "ppm"
}, {
"Timestamp": "2021-11-01T14:00:00Z",
"Value": 14.8,
"UnitsAbbreviation": "ppm"
}, {
"Timestamp": "2021-11-08T15:00:00Z",
"Value": 20.1,
"UnitsAbbreviation": "ppm"
}, {
"Timestamp": "2021-11-15T15:00:00Z",
"Value": 19.5,
"UnitsAbbreviation": "ppm"
}, {
"Timestamp": "2021-11-22T15:00:00Z",
"Value": 19.7,
"UnitsAbbreviation": "ppm"
}, {
"Timestamp": "2021-11-29T15:00:00Z",
"Value": 20.4,
"UnitsAbbreviation": "ppm"
}
]
}
}
}

最佳答案

当json结构的某些部分未知时,您需要手动探索它。有多种方法可以做到这一点,但我习惯于传递 JObject :

var root = JObject.Parse(text);
// Unknown name property, then iterate
// When the object has one property, First is a good fit
var response = root.First as JProperty;
var guid = response.Name;
var itemsJson = response.Value["Content"]["Items"];
var items = itemsJson.ToObject<List<Item>>();

关于c# - 如何将具有动态根名称的 json 转换为 C# 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74086940/

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