我只是想从 json 数组中读取 json 对象。但我得到[Object]
在尝试读取整个数组时代替嵌套的 Json 对象。
我的 JSON 如下所示:
var json = {
"root_id": [
{
"child-id1": {
"name": "name1",
"created_by": null,
"created_at": "2018-05-30T19:34:38.657Z",
"configs": {
"generic": {
"size": 1000,
"timeout": 60,
"field1_key": "field1_value"
},
"specific": {
"key1": "xxx-xxx",
"field1_key": "field1_value"
}
}
}
},
{
"child-id2": {
"name": "name2",
"created_by": null,
"created_at": "2018-05-30T19:34:38.657Z",
"configs": {
"generic": {
"size": 10,
"timeout": 60,
"field1_key": "field1_value"
},
"specific": {
"key1": "xxx-xxx",
"field1_key": "field1_value"
}
}
}
}
]
}
我希望我的函数返回“root_id”对象的 Json 数组。所以,我只是尝试了这个简单的代码来读取数组:
var val = json['root_id'];
console.log(val);
但它返回这个:
[ { 'child-id1':
{ name: 'name1',
created_by: null,
created_at: '2018-05-30T19:34:38.657Z',
configs: [Object] } },
{ 'child-id2':
{ name: 'name2',
created_by: null,
created_at: '2018-05-30T19:34:38.657Z',
configs: [Object] } } ]
如何确保嵌套对象按原样返回,而不仅仅是 [object]?
最佳答案
如果您在服务器中使用此 console.log
,则应使用 util.inspect()
, https://nodejs.org/api/util.html .
检查这个问题:How can I get the full object in Node.js's console.log(), rather than '[Object]'?
如果您在浏览器中使用,它应该显示所有内容。
关于javascript - NodeJS 中嵌套的 JSON 对象返回 [Object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51251949/