Output of json not correct

javascript json node.js

33 观看

1回复

8 作者的声誉

I am trying to use JSON.stringify to make my json object readable.
However, it is not working. Could anyone kindly help me to see what I have done wrong?

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);

The output should be along the lines of:

[
  {
    "days": [
      0,
      3,
      26,
      20,
      39,
      1,
      0
    ],
    "total": 89,
    "week": 1336280400
  }
]

according to documentation:

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

However, I am getting:

"[{\"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}]"
作者: slayallevil 的来源 发布者: 2017 年 12 月 27 日

回应 (1)


0

1231 作者的声誉

决定

The variable body in your callback function contains a String value (not a valid JSON itself).
That is why JSON.stringify() could not format it the way you want.

You should convert it to JSON first using the JSON.parse() function.

Here is you code, with the mentioned function applied to 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);
作者: Kayvan Mazaheri 发布者: 27.12.2017 06:27
32x32