gpt4 book ai didi

javascript - 无法获取 JSON Key 的值,仅返回 Key

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

过去几个月在 Google Go 上进行了大量工作后,我又回到了 Express 堆栈。我正在构建简单的 web 应用程序页面来工作,迭代 JSON 对象。我正在尝试打印作为较大对象一部分的对象中每个键的值,但只记录该键?

这是我读过的 data.json(整个对象中仅显示 1 个对象,为了安全起见省略了数据。):

{
"Prod18r2" : {
"baseAPIURL" : "https://apiurl.com/",
"serviceEndpoints" : {
"restaurantManager" : "https://prodapiurl.com/rm/",
"transactionManager" : "https://prodapiurl.com/rm/",
"userManager" : "https://prodapiurl.com/rm/",
"recordHistory" : "https://prodapiurl.com/rm/"
}
}
}

这是我现在在 test.js 文件中的迭代和日志逻辑:

for(var x in INSTANCES.Prod18r2.serviceEndpoints) {
console.log(x);
}

输出:

PS C:\Users\payton.juneau\Desktop\Me\Projects\Node\etm-scry-webapp>node .\test.js restaurantManager transactionManager userManager recordHistory PS C:\Users\payton.juneau\Desktop\Me\Projects\Node\etm-scry-webapp>

提前感谢您的回复,我知道这可能是令人尴尬的愚蠢。

最佳答案

它需要object[keyName]来获取值

var INSTANCES = {
"Prod18r2": {
"baseAPIURL": "https://apiurl.com/",
"serviceEndpoints": {
"restaurantManager": "https://prodapiurl.com/rm/",
"transactionManager": "https://prodapiurl.com/rm/",
"userManager": "https://prodapiurl.com/rm/",
"recordHistory": "https://prodapiurl.com/rm/"
}
}
}
//Here is my iterate-and-log logic right now in a test.js file:

for (var x in INSTANCES.Prod18r2.serviceEndpoints) {
console.log(INSTANCES.Prod18r2.serviceEndpoints[x]);
}

关于javascript - 无法获取 JSON Key 的值,仅返回 Key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49561423/

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