json的输出不正确

时间:2017-12-27 17:27:15

标签: javascript json node.js

我正在尝试使用JSON.stringify使我的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}]"

1 个答案:

答案 0 :(得分:0)

回调函数中的变量 body 包含 String 值( 不是有效的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);