在NodeJs中将此缓冲区转换为JSON

时间:2017-06-26 19:03:36

标签: javascript buffer

我在NodeJs中发出了一个HTTPS POST请求,我正在找回一个应该是JSON的奇怪缓冲区。

这是我的代码:

       var options = {
            url: someURL,
            host: "SomeApi.com",
            method: 'POST',
            encoding: null,
            gzip: true,
            headers: {
                'Accept': 'application/json, text/json, text/x-json, text/javascript, text/xml, application/xml',
                'User-Agent': 'RestSharp/105.2.3.0',
                'Accept-Encoding': 'gzip, deflate',
                'Content-Length': '0'
            },
        }

        var body = "";
        https.get(options, (response) => {
            response.on('data', (chunk) => {
                body += chunk;
            });
            response.on('end', () => {
                //var data = JSON.parse(body);
                console.log(response);
                //gunzipJSON(response)
            });

        });

以下是我要回来的事情:

....
 read: [Function] }
{"type":"Buffer","data":[123,13,10,32,32,34,97,99,99,101,115,115,95,116,111,107,
101,110,34,58,34,100,101,49,56,51,100,100,57,45,102,49,98,52,45,52,53,98,53,45,9
7,98,98,100,45,54,55,56,98,52,102,99,99,100,56,57,56,34,44,13,10,32,32,34,116,11
1,107,101,110,95,116,121,112,101,34,58,34,109,97,99,34,44,13,10,32,32,34,101,120
,112,105,114,101,115,95,105,110,34,58,51,54,48,48,44,13,10,32,32,34,114,101,102,
114,101,115,104,95,116,111,107,101,110,34,58,34,48,56,102,51,54,50,99,55,45,57,5
2,56,100,45,52,52,101,54,45,98,53,97,51,45,50,56,50,54,54,54,102,97,48,48,57,55,
34,44,13,10,32,32,34,115,99,111,112,101,34,58,34,111,111,98,34,44,13,10,32,32,34
,109,97,99,95,107,101,121,34,58,34,48,51,99,102,52,97,51,54,45,51,97,57,50,45,52
,102,51,98,45,56,97,54,98,45,101,56,100,98,101,56,56,98,102,100,54,51,34,44,13,1
0,32,32,34,109,97,99,95,97,108,103,111,114,105,116,104,109,34,58,34,104,109,97,9
9,45,115,104,97,45,49,34,44,13,10,32,32,34,97,117,116,104,111,114,105,122,101,10
0,95,99,111,110,116,97,99,116,95,105,100,34,58,34,53,57,48,48,56,52,34,44,13,10,
32,32,34,97,117,116,104,111,114,105,122,101,100,95,99,111,110,115,117,108,116,97
,110,116,95,107,101,121,34,58,34,54,50,97,49,100,48,50,56,45,50,102,55,100,45,52
,57,55,48,45,98,102,53,52,45,102,99,50,51,97,97,51,48,99,50,56,99,34,44,13,10,32
,32,34,97,117,116,104,111,114,105,122,101,100,95,99,111,110,115,117,108,116,97,1
10,116,95,115,117,102,102,105,120,34,58,34,37,50,50,85,83,37,50,50,34,44,13,10,3
2,32,34,97,117,116,104,111,114,105,122,101,100,95,99,111,110,115,117,108,116,97,
110,116,95,115,117,98,115,105,100,105,97,114,121,34,58,34,85,83,34,13,10,125]}

这是我试图找回的JSON代码的长编码表示吗?如果是这样,我该如何解码?

如果这是一个补救问题,我提前道歉。我是NodeJs的新手并提出请求,我感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

nodejs.org/api/stream.html#stream_readable_setencoding_encod‌​ing



var options = {
  url: someURL,
  host: "SomeApi.com",
  method: 'POST',
  encoding: null,
  gzip: true,
  headers: {
    'Accept': 'application/json, text/json, text/x-json, text/javascript, text/xml, application/xml',
    'User-Agent': 'RestSharp/105.2.3.0',
    'Accept-Encoding': 'gzip, deflate',
    'Content-Length': '0'
  },
}

var body = "";
https.get(options, (response) => {
  response.setEncoding('utf8');
  response.on('data', (chunk) => {
    body += chunk;
  });
  response.on('end', () => {
    //var data = JSON.parse(body);
    console.log(response);
    //gunzipJSON(response)
  });

});