gpt4 book ai didi

c# - 在 C# 中从复杂的 JSON 结构中提取数据

转载 作者:太空狗 更新时间:2023-10-30 00:42:42 25 4
gpt4 key购买 nike

我有如下所示的 JSON 数据:

{
"position":[
{
"top":[
42,
12
]
},
{
"middle":[
10,
15
]
},
{
"bottom":[
5,
201
]
},
{
"axis":[
{
"X":[
901,
51,
67
]
},
{
"Y":[
1474186,
561647
]
},
{
"Z":[
911,
1296501
]
},
15,
20
]
}
],
"validated":true,
"metadata":{
"uri":"/data/complex/",
"session":[
1818,
14
],
"private":false
},
"vists":0,
"ok":true,
"data":{
"10":{
"title":"Alpha",
"author":"Justin X. Ample",
"cover":"/f48hf58.tiff"
},
"901":{
"title":"Tau",
"author":"Felina Blank",
"cover":"/45trthrref.tiff"
}
},
"live":null
}

根据这些数据,我想显示如下列表:

Alpha, Justin X. Ample
Tau, Felina Blank

请注意,键(在我的示例中为 10 和 901)是不可预测的。所以我想以某种方式创建一个表示“数据”结构的对象并对其进行迭代以获取每个条目的标题和作者。

使用基本的 JSON 结构,我成功地完成了类似这样的事情(使用 JSON.NET):

public class Foo
{
public int bar { get; set; }
public string baz { get; set; }
public string quxx { get; set; }
}

...

// json = {"bar": 1, "baz":"two", "quxx":"three"}
var result = await JsonConvert.DeserializeObjectAsync<Foo>(json);

return result.baz // "two"

但我不知道我需要做什么才能使其适用于复杂的结构。

最佳答案

var jObj = JsonConvert.DeserializeObject(json) as JObject;
var result = jObj["data"].Children()
.Cast<JProperty>()
.Select(x => new {
Title = (string)x.Value["title"] ,
Author = (string)x.Value["author"],
})
.ToList();

关于c# - 在 C# 中从复杂的 JSON 结构中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14025732/

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