javascript - JSON 正在返回一个数字数组,而不是 JSON 中的对象中的值
问题描述
我正在尝试通过这个 JSON 对象并从中获取一些值。
let currentPage = "
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "xaswoie0ncrg"
}
},
"id": "7lqAYzwP92G9TMDBUVnadp",
"type": "Entry",
"createdAt": "2020-07-30T18:08:33.159Z",
"updatedAt": "2020-07-30T18:22:50.276Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 2,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "landingPage"
}
},
"locale": "en-US"
},
"fields": {
"pageTitle": "Leading the next generation of renewable fuels",
"heroImage": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "vnjfnYzSyhqOjKlmNmBGb"
}
},
"pageZone": [
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "3aQvORUYowW0SoofuvHUov"
}
},
{
"sys": {
"type": "Link",
"linkType": "Entry",
"id": "Qfj1hNJ9euSkBcAQEDaN5"
}
}
]
}
}"
然后我解析 JSON:
let currentPage2 = JSON.parse(currentPage);
现在问题来了。如果在控制台中记录:
console.log(Object.keys(currentPage2.fields.pageZone[0].sys.id));
节点在终端中返回:
[
'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9',
'10', '11', '12', '13', '14',
'15', '16', '17', '18', '19',
'20', '21'
]
我想用这个:
console.log(Object.keys(currentPage2.fields.pageZone[0].sys.id).value);
//with expected value of "3aQvORUYowW0SoofuvHUov"
相反,它返回undefined
. 我不知道为什么会这样。我曾尝试使用JSON.stringify
etc 并再次对其进行解析,但它仍然以这种方式运行。
解决方案
只需使用currentPage2.fields.pageZone[0].sys.id
. 根本不需要Object.keys
,除非您想要字符串的每个索引。
推荐阅读
- sql - 自加入因 where 子句而失败
- qt - QML 中的 ScreenRayCaster
- php - 从多个数组中获取项目的每个组合 - PHP
- android - 在使用 Retrofit 解析 pojo 类时出现错误,其中 arraylist 中有另一个 pojo 类
- c# - .Net 中是否有一个类来存储一个值和一个先前的值?
- sapui5 - Can't open sapui5 application extension from fiori launchpad
- java - 无法使用所有新的 Kinesis 消息
- ruby - 在无头 Chrome 中调试
- node.js - Firestore:如何从集合中获取最新更新的文档
- angular - 如何消除对角度输入的关注?