最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

removing the backslashes in json string using the javascript - Stack Overflow

programmeradmin4浏览0评论

i have JSON response which is having backslashes and some responses are not containing the backslashes.

I need to show error message based on the response, How do i parse the JSON response using javascript?

JSON response with out backslashes,

{"_body":{"isTrusted":true},"status":0,"ok":false,"statusText":"","headers":{},"type":3,"url":null} 

response with backslashes,

{"_body":"{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"}","status":500,"ok":false,"statusText":"Internal Server Error"}

i tried in the following way but it is working only for JSON response which is not having the backslashes.

var strj = JSON.stringify(err._body);
 var errorobjs = strj.replace(/\\/g, "");

i have JSON response which is having backslashes and some responses are not containing the backslashes.

I need to show error message based on the response, How do i parse the JSON response using javascript?

JSON response with out backslashes,

{"_body":{"isTrusted":true},"status":0,"ok":false,"statusText":"","headers":{},"type":3,"url":null} 

response with backslashes,

{"_body":"{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"}","status":500,"ok":false,"statusText":"Internal Server Error"}

i tried in the following way but it is working only for JSON response which is not having the backslashes.

var strj = JSON.stringify(err._body);
 var errorobjs = strj.replace(/\\/g, "");
Share Improve this question asked Nov 18, 2016 at 4:58 vishnuvishnu 4,59916 gold badges62 silver badges91 bronze badges 6
  • if it has backslashes its not a valid json but a string – madalinivascu Commented Nov 18, 2016 at 5:02
  • 1 @madalinivascu backslashes are valid in json.The problem is not backslashes but, the json format is invalid. He is wrapping obj in string. – Atul Sharma Commented Nov 18, 2016 at 5:15
  • 1 Both of the above are valid json. The second one just has a field that contains a json string itself, and so needs to be JSON parsed again. See my answer. – Joe Commented Nov 18, 2016 at 5:17
  • 1 @Joe but, that's never a good option .. to parse json and read obj as string and then parse it again. – Atul Sharma Commented Nov 18, 2016 at 5:18
  • 1 I agree, but we have no idea if this person controls the server code at all. We can't just assume he can change the server code, we need to provide an answer given the constraints. – Joe Commented Nov 18, 2016 at 5:20
 |  Show 1 more ment

3 Answers 3

Reset to default 3

Actually the problem is not with / slashs. The JSON is INVALID.

remove these " from backend server

{"_body":"{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"}","status":500,"ok":false,"statusText":"Internal Server Error"}

double quote before "{"timestamp and one after login"}" these two highlighted and your code will work.

var data = '{"_body":{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"},"status":500,"ok":false,"statusText":"Internal Server Error"}';

var json_data = JSON.parse(data);

console.log(json_data);

You are actually wrapping body object in string at backend which is not valid.

"body" : "bodyOBJ"  //Invalid
"body" : bodyObj    //Valid
var obj = JSON.parse(response)

if(typeof obj._body == "string") {
    obj._body = JSON.parse(obj._body)
}

console.log(obj);

Solution:

var response = {"_body":"{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"}","status":500,"ok":false,"statusText":"Internal Server Error"};
var body = JSON.parse(response._body);
console.log(body.error);

Explanation:

You have a top level object with one key, _body. The value of that key is a string containing JSON itself. This is usually because the server side code didn't properly create the JSON. That's why you see the \" inside the string. Unless you are able to fix the server side code, you have to decode the nested JSON manually.

发布评论

评论列表(0)

  1. 暂无评论