Node.js 为什么我能';是否使用Nodejs检索json数据?

Node.js 为什么我能';是否使用Nodejs检索json数据?,node.js,Node.js,我只需要一种从特定url检索json数据的方法。 我写了这个程序: 'use strict'; var http = require('http'); var request = require("request"); var url = "https://restcountries.eu/rest/v2/name/united" var server = http.createServer(function (req, res) { res.writeHead(200, {'Cont

我只需要一种从特定url检索json数据的方法。 我写了这个程序:

'use strict';
var http = require('http');
var request = require("request");

var url = "https://restcountries.eu/rest/v2/name/united"


var server = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});

  request({
      url: url,
      json: true
  }, function (error, response, body) {
      if (!error && response.statusCode === 200) {
         res.write(JSON.parse(body)) // Print the json response
      }else{
         res.write("error");
         res.end();
      }
  })


})

server.listen(1338, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1338/');
但我有一个错误:

# node mytest.js
Server running at http://127.0.0.1:1338/
undefined:1
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
 ^

SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Request._callback (/home/xxx/Nodejs/Esempi/emilianotest2.js:18:25)
    at Request.self.callback (/home/xxx/Nodejs/Esempi/node_modules/request/request.js:185:22)
    at Request.emit (events.js:160:13)
    at Request.<anonymous> (/home/xxx/Nodejs/Esempi/node_modules/request/request.js:1161:10)
    at Request.emit (events.js:160:13)
    at IncomingMessage.<anonymous> (/home/xxx/Nodejs/Esempi/node_modules/request/request.js:1083:12)
    at Object.onceWrapper (events.js:255:19)
    at IncomingMessage.emit (events.js:165:20)
    at endReadableNT (_stream_readable.js:1101:12)

因为您给出了参数
json:true
request
已经为您解析了它。然后,当您将not-JSON-any-more数组传递到
JSON.parse
中时,它会在被解析之前变成一个字符串;数组中的对象获得了熟悉的
[object object]
表示,而
JSON.parse
会阻塞它,因为
[object object]
看起来不像一个合适的数组

试试看{
让json=json.stringify([{a:1}])
log(“解析一次:”);
log(JSON.parse(JSON));
log(“解析两次:”);
log(JSON.parse(JSON.parse(JSON));
}捕获(e){
控制台错误(e.message);

}
因为您提供了参数
json:true
请求
已经为您解析了它。然后,当您将not-JSON-any-more数组传递到
JSON.parse
中时,它会在被解析之前变成一个字符串;数组中的对象获得了熟悉的
[object object]
表示,而
JSON.parse
会阻塞它,因为
[object object]
看起来不像一个合适的数组

试试看{
让json=json.stringify([{a:1}])
log(“解析一次:”);
log(JSON.parse(JSON));
log(“解析两次:”);
log(JSON.parse(JSON.parse(JSON));
}捕获(e){
控制台错误(e.message);

}
看看身体里面有什么。并且
res.write
接受字符串或缓冲区而不是普通对象。JSON中意外的标记o几乎总是意味着将非JSON数据传递给JSON.parse()。检查请求附带的
body
变量是否包含有效的JSON。查看
body
中的内容。并且
res.write
接受字符串或缓冲区而不是普通对象。JSON中意外的标记o几乎总是意味着将非JSON数据传递给JSON.parse()。检查请求随附的
body
变量是否包含有效的JSON。
Server running at http://127.0.0.1:1338/
_http_outgoing.js:651
    throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument',
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string or Buffer
    at write_ (_http_outgoing.js:651:11)
    at ServerResponse.write (_http_outgoing.js:626:10)