Joe*_*ing 0 javascript json node.js
当我使用JSON.parse并注销该require模块获取的某些数据时,将记录嵌套对象[Object]。下面是一个示例(当前使用Node版本10.15):
const request = require("request");
const url = "https://www.reddit.com/r/javascript.json";
request(url, (error, response) => {
const data = JSON.parse(response.body);
console.log(data)
});Run Code Online (Sandbox Code Playgroud)
{ kind: 'Listing',
data:
{ modhash: '',
dist: 26,
children:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
after: 't3_bf4cl6',
before: null } }Run Code Online (Sandbox Code Playgroud)
我在看这个问题:JSON.parse从JSON返回[对象]
问的人很好奇为什么在JSON.parse之后,对象被记录为[Object]。最高答案指出JSON.parse隐藏数据的原因是为了提高可读性。但是答案并没有说明替代默认行为的方法。
如何获取JSON.parse记录完整数据?有没有办法覆盖默认行为?
如果response.body直接记录未按照您喜欢的方式进行格式化,这就是您这样做的原因JSON.parse,那么要记录完整格式的对象,
console.log(JSON.stringify(JSON.parse(response.body), null, 2))
| 归档时间: |
|
| 查看次数: |
67 次 |
| 最近记录: |