gpt4 book ai didi

javascript - 使用 javascript 访问 JSON 对象中的数据

转载 作者:行者123 更新时间:2023-12-01 02:44:16 26 4
gpt4 key购买 nike

我返回了以下 JSON 对象,并且我想访问所有“标题”值。我已经能够获得所有“页面”的新对象,但不能获得“标题”。我已经尝试了一切,除了正确的事情!这里的“类似”问题是直接的数组访问问题。这似乎涉及嵌套数组。请帮忙!谢谢。

{
"batchcomplete": "",
"continue": {
"gsroffset": 15,
"continue": "gsroffset||"
},
"query": {
"pages": {
"22989": {
"pageid": 22989,
"ns": 0,
"title": "Paris",
"index": 1
},
"59134": {
"pageid": 59134,
"ns": 0,
"title": "Paris Commune",
"index": 13
},
"61371": {
"pageid": 61371,
"ns": 0,
"title": "Paris (disambiguation)",
"index": 2
},
"64129": {
"pageid": 64129,
"ns": 0,
"title": "Catacombs of Paris",
"index": 11
},
"76286": {
"pageid": 76286,
"ns": 0,
"title": "Disneyland Paris",
"index": 15
},
"89106": {
"pageid": 89106,
"ns": 0,
"title": "Paris–Brest–Paris",
"index": 8
},
"357488": {
"pageid": 357488,
"ns": 0,
"title": "Paris Saint-Germain F.C.",
"index": 10
},
"868936": {
"pageid": 868936,
"ns": 0,
"title": "Paris Gun",
"index": 14
},
"2397134": {
"pageid": 2397134,
"ns": 0,
"title": "Paris (Paris Hilton album)",
"index": 6
},
"7618874": {
"pageid": 7618874,
"ns": 0,
"title": "Paris syndrome",
"index": 9
},
"11217925": {
"pageid": 11217925,
"ns": 0,
"title": "Paris Hilton",
"index": 7
},
"23528038": {
"pageid": 23528038,
"ns": 0,
"title": "Paris Jackson (actress)",
"index": 5
},
"30242372": {
"pageid": 30242372,
"ns": 0,
"title": "Paris Agreement",
"index": 4
},
"45259235": {
"pageid": 45259235,
"ns": 0,
"title": "Paris Laxmi",
"index": 12
},
"55340805": {
"pageid": 55340805,
"ns": 0,
"title": "Paris Paris",
"index": 3
}
}
}
}

它希望我添加更多详细信息,但我不知道还可以添加什么。

最佳答案

试试这个:

const data = //your json
const pages = data.query.pages
const titles = Object.keys(pages).map( key => pages[key].title )

将“pages”中的值存储在变量中,然后使用 Object.keys 将“pages”中的所有键作为数组获取,然后使用映射运算符迭代这些键,并为每个键返回属性“该条目的标题。

关于javascript - 使用 javascript 访问 JSON 对象中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47356108/

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