我试图从以下回复中提取响应
"{"Message":"Looks like you need to login"}"
我试着把它串起来如下
var response = "{"Message":"Looks like you need to login"}";
var json_response = JSON.stringify(response);
但我的回答最终看起来像这样。
"{\"Message\":\"Looks like you need to login\"}"
有关为何发生这种情况的任何想法?以及如何通过执行类似
的操作来提取消息 或许 json_response.Message
?
答案 0 :(得分:3)
您需要使用JSON.parse()
:
var str = "{\"Message\":\"Looks like you need to login\"}";
var json = JSON.parse(str);
console.log(json.Message);
答案 1 :(得分:0)
试试这个:
var response = { Message: "Looks like you need to login" } ;
var json_response = JSON.stringify( response ) ;
答案 2 :(得分:0)
你需要使用json的parse()方法,这是非常有用的。所以继续使用它是非常轻量级的。
以下是我的回答:
var myString = "{\"Message\":\"Looks like you need to login\"}";
var parsedJson = JSON.parse(myString);
alert(parsedJson.Message);