json的输出不正确

我正在尝试使用JSON.stringify来使我的json对象可读。
但是,它不起作用。任何人都可以帮助我看看我做错了什么吗?json的输出不正确

var request = require('request'); 

var options = {

url: 'https://api.github.com/repos/benjaminsunderland/TicTacToe/stats/commit_activity',

headers: {

'User-Agent': 'request'

}

};

function callback(error, response, body) {

console.log(JSON.stringify(body, null, 2));

}

request(options, callback);

输出应沿着线:根据文件

[ 

{

"days": [

0,

3,

26,

20,

39,

1,

0

],

"total": 89,

"week": 1336280400

}

]

https://developer.github.com/v3/repos/statistics/

但是,我得到:

"[{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1483232400},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1483837200},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1484442000},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1485046800},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1485651600},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1486256400},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1486861200},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1487466000},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1488070800},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1488675600},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1489280400},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1489881600},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1490486400},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1491091200},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1491696000},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1492300800},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1492905600},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1493510400},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1494115200},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1494720000},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1495324800},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1495929600},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1496534400},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1497139200},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1497744000},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1498348800},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1498953600},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1499558400},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1500163200},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1500768000},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1501372800},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1501977600},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1502582400},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1503187200},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1503792000},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1504396800},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1505001600},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1505606400},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1506211200},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1506816000},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1507420800},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1508025600},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1508630400},{\"days\":[0,0,0,0,5,2,6],\"total\":13,\"week\":1509235200},{\"days\":[1,0,0,0,0,0,0],\"total\":1,\"week\":1509840000},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1510444800},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1511049600},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1511654400},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1512259200},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1512864000},{\"days\":[0,1,0,0,0,0,0],\"total\":1,\"week\":1513468800},{\"days\":[0,0,0,0,0,0,0],\"total\":0,\"week\":1514073600}]" 

回答:

在你的回调函数的变量body包含字符串值(不是一个有效的JSON本身)。
这就是为什么JSON.stringify()无法以您想要的方式进行格式化的原因。

您应该先使用JSON.parse()函数将其转换为JSON。

这里是你的代码,提供给body所提到的功能:

var request = require('request'); 

var options = {

url: 'https://api.github.com/repos/benjaminsunderland/TicTacToe/stats/commit_activity',

headers: {

'User-Agent': 'request'

}

};

function callback(error, response, body) {

var jsonBody = JSON.parse(body);

console.log(JSON.stringify(jsonBody, null, 2));

}

request(options, callback);

以上是 json的输出不正确 的全部内容, 来源链接: utcz.com/qa/266599.html

回到顶部