javascript - NodeJS 中嵌套的 JSON 对象返回 [Object]

标签 javascript json node.js

我只是想从 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/

相关文章:

javascript - Angular Routing ngRoute 无法提取我的其他 HTML 文件

javascript - 如何获取数组内的数组?

javascript - 在文本框中按下退格键时发出警报

javascript:我如何制作自己的测试模块?

ruby - Yajl::ParseError:词法错误:json 文本中的字符无效

javascript - Sails.js 简单的私有(private)消息传递(发布/订阅和模型)

node.js - express 4.0 中的 'extended' 是什么意思?

javascript - 使用 socket.emit 处理 Node.js 错误

javascript - 使用 JS 禁用选项

c++ - 如何将elasticsearch的数据保存为json格式?