简体   繁体   English

node.js中的JSON.parse意外结果

[英]JSON.parse unexpected result in node.js

I am sending a http request to a website (from node.js) which returns a JSON object. 我正在向网站(来自node.js)发送一个http请求,该网站返回一个JSON对象。 I get the expected JSON file. 我得到了预期的JSON文件。 However when I parse the JSON text, my program isn't able to do anything. 但是,当我解析JSON文本时,我的程序无法执行任何操作。

var URL = 'http://www.omdbapi.com/?t=' + movie + '&y=&plot=short&r=json';
requestify.get(URL).then(function (response) {
    console.log(response.getBody()); // It prints correctly
    var jsonBody =  response.getBody();
    var jsonObject = JSON.parse(jsonBody);
    if (jsonObject.Response == 'False') {
        console.log('False'); //not printed
    } else {
        console.log('true'); //Not printed
    }
});

Sample JSON output: 示例JSON输出:

{"Response":"False","Error":"Movie not found!"}

response.body is the raw text response. response.body是原始文本响应。 response.getBody() should already return a parsed JSON response as long as you have the correct content-type header specified. 只要您指定了正确的content-type标头, response.getBody()应该已经返回已解析的JSON响应。

Sending a JS object to JSON.parse results in a SyntaxError . 将JS对象发送到JSON.parse导致SyntaxError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM