Nodejs从http请求块读取JSON数据

Dom*_*Dom 2 rest json node.js

我正在使用Jira API来获取单张票据的数据.我已经成功地向服务器设置了一个http GET请求,并且可以向控制台显示数据,但理想情况下我需要从JSON格式的数据中获取某些属性.

当我尝试阅读属性时,我只是未定义.

var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);         // This displays the JSON
    console.log('endSTATUS: ' + chunk.id); // This shows up undefined
});    
Run Code Online (Sandbox Code Playgroud)

该数据是在从JIRA API格式以供参考.res中的第一个控制台日志成功显示了块中的所有数据.第二个是:

endSTATUS: undefined
Run Code Online (Sandbox Code Playgroud)

luc*_*lho 6

在数据流完成后尝试获取正文.像这样:

        var body = '';
        response.on('data', function(d) {
            body += d;
        });
        response.on('end', function() {

            // Data reception is done, do whatever with it!
            var parsed = JSON.parse(body);
            console.log('endSTATUS: ' + parsed.id);
        });
Run Code Online (Sandbox Code Playgroud)