【发布时间】:2018-12-17 12:22:28
【问题描述】:
我只是想从 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显示的内容?如果您想在控制台中显示所有内容,请 JSON.stringify 它。 -
console.log显示[Object]当对象向下三层时。您可以使用console.log(JSON.stringify(yourObject)),这可能会显示整个 JSON。 -
console.log(util.inspect(val, { compact: true, depth: 15, breakLength: 80 }));
标签: javascript json node.js