Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t获取JSON键的值,只返回该键_Javascript_Json_Node.js_Express - Fatal编程技术网

Javascript Can';t获取JSON键的值,只返回该键

Javascript Can';t获取JSON键的值,只返回该键,javascript,json,node.js,express,Javascript,Json,Node.js,Express,过去几个月,我在Google Go上做了很多工作,现在又回到了Express stack。我正在为工作构建简单的webapp页面,迭代JSON对象。我试图打印作为较大对象一部分的对象中每个键的值,但只记录该键 这是我读过的data.json(在整个对象中只显示1个对象,为了安全起见省略了数据): 下面是我目前在test.js文件中的迭代和日志逻辑: for(var x in INSTANCES.Prod18r2.serviceEndpoints) { console.log(x); }

过去几个月,我在Google Go上做了很多工作,现在又回到了Express stack。我正在为工作构建简单的webapp页面,迭代JSON对象。我试图打印作为较大对象一部分的对象中每个键的值,但只记录该键

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

下面是我目前在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用户管理器 历史记录 C:\Users\payton.juneau\Desktop\Me\Projects\Node\etm scry webapp>


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

需要
object[keyName]
才能获得值

var实例={
“Prod18r2”:{
“baseAPIURL”:https://apiurl.com/",
“serviceEndpoints”:{
“餐厅经理”:https://prodapiurl.com/rm/",
“transactionManager”:https://prodapiurl.com/rm/",
“用户管理器”:https://prodapiurl.com/rm/",
“记录历史”:https://prodapiurl.com/rm/"
}
}
}
//下面是我目前在test.js文件中的迭代和日志逻辑:
for(INSTANCES.Prod18r2.serviceEndpoints中的var x){
log(INSTANCES.Prod18r2.serviceEndpoints[x]);
}
您应该

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

我将您的“x”替换为“serviceEndpoints”,并使用返回的键获取json值。

该值为
INSTANCES.Prod18r2.serviceEndpoints[x]
。您可能想看看
Object.entries
,它为您提供了键值对。@Barmar哇,您做到了!这让我很困惑,因为它不是JSON中的数组。它是嵌套对象。而且,我认为我的逻辑是有道理的,因为。。哦对于嵌套对象中的每个键,打印该键。。我写了一半才明白。。无论如何,谢谢@乔纳斯。嘿,我在四处搜索的时候看到了这个,但是我把它扔了,因为我不认为这正是我需要的解决方案,尽管我应该读一下,看看它是否有帮助。是的<代码>对于对象的(const[key,value])项(someObj))非常有用。。。
for(var serviceEndpoints in INSTANCES.Prod18r2.serviceEndpoints) {
    console.log(INSTANCES.Prod18r2.serviceEndpoints[serviceEndpoints]);
}