【问题标题】:Nested JSON objects returns [Object] in NodeJS [duplicate]嵌套的 JSON 对象在 NodeJS 中返回 [Object] [重复]
【发布时间】: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


【解决方案1】:

如果你在服务器中使用这个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]'?

如果您在浏览器中使用它应该会显示所有内容。

【讨论】:

    【解决方案2】:

    试试

    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"
              }
            }
          }
        }
      ]
    }
     
    var count = Object.keys(json['root_id']).length;
    for (var i = 0; i < count; i++) {
        console.log(json['root_id'][i]);
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多