Using node.js 6.10
使用node.js 6.10
console.log("returned data1 " + body);
// returns: returned data1 "{'response' : 'Not latest version of file, update not performed'}"
I want to extract the Not latest version of file, update not performed
and place it into the valueReturned var. I have tried
我想提取不是最新版本的文件,更新未执行并将其放入valueReturned var。我努力了
var jsonBody = JSON.parse(body);
var valueReturned = jsonBody["response"];
console.log("logs: " + valueReturned);
// returns: logs: undefined
does anyone know where I am going wrong?
有谁知道我哪里出错了?
ThankYous
ThankYous
3 个解决方案
#1
2
You need a valid JSON string to parse JSON.
您需要一个有效的JSON字符串来解析JSON。
let body = "{ \"response\" : \"Not latest version of file, update not performed\"}";
let jsonBody = JSON.parse(body);
let valueReturned = jsonBody.response;
console.log("logs: " + valueReturned);
#2
0
The problem is body is not a valid JSON format. The key and value should be wrapped in a double quotes("). Otherwise the code should work as shown below:
问题是body不是有效的JSON格式。键和值应该用双引号(“)包装。否则代码应该如下所示:
const body = '{ "response" : "Not latest version of file, update not performed" }';
console.log("returned data1 " + body);
// returns: returned data1 "{'response' : 'Not latest version of file, update not performed'}"
var jsonBody = JSON.parse(body);
var valueReturned = jsonBody["response"];
console.log("logs: " + valueReturned);
// returns: logs: undefined
#3
0
A hacky and unreliable method if you have to deal with your body
as is would be to manually transform it into JSON format and then parse it:
如果你必须处理你的身体,那么一种hacky和不可靠的方法是手动将其转换为JSON格式然后解析它:
const body = `"{'response' : 'Not latest version of file, update not performed'}"`;
const formattedBody = body
.slice(1, body.length - 1)
.replace(/'/g, '"');
const obj = JSON.parse(formattedBody);
console.log(obj.response);
#1
2
You need a valid JSON string to parse JSON.
您需要一个有效的JSON字符串来解析JSON。
let body = "{ \"response\" : \"Not latest version of file, update not performed\"}";
let jsonBody = JSON.parse(body);
let valueReturned = jsonBody.response;
console.log("logs: " + valueReturned);
#2
0
The problem is body is not a valid JSON format. The key and value should be wrapped in a double quotes("). Otherwise the code should work as shown below:
问题是body不是有效的JSON格式。键和值应该用双引号(“)包装。否则代码应该如下所示:
const body = '{ "response" : "Not latest version of file, update not performed" }';
console.log("returned data1 " + body);
// returns: returned data1 "{'response' : 'Not latest version of file, update not performed'}"
var jsonBody = JSON.parse(body);
var valueReturned = jsonBody["response"];
console.log("logs: " + valueReturned);
// returns: logs: undefined
#3
0
A hacky and unreliable method if you have to deal with your body
as is would be to manually transform it into JSON format and then parse it:
如果你必须处理你的身体,那么一种hacky和不可靠的方法是手动将其转换为JSON格式然后解析它:
const body = `"{'response' : 'Not latest version of file, update not performed'}"`;
const formattedBody = body
.slice(1, body.length - 1)
.replace(/'/g, '"');
const obj = JSON.parse(formattedBody);
console.log(obj.response);