I'm trying to extract just the response from the following response
"{"Message":"Looks like you need to login"}"
I tried to stringyfy it as follows
var response = "{"Message":"Looks like you need to login"}";
var json_response = JSON.stringify(response);
But my response ends up looking something like this.
"{\"Message\":\"Looks like you need to login\"}"
Any ideas on why this is happening? and how I can extract just the message by doing something like
json_response.Message
perhaps?
I'm trying to extract just the response from the following response
"{"Message":"Looks like you need to login"}"
I tried to stringyfy it as follows
var response = "{"Message":"Looks like you need to login"}";
var json_response = JSON.stringify(response);
But my response ends up looking something like this.
"{\"Message\":\"Looks like you need to login\"}"
Any ideas on why this is happening? and how I can extract just the message by doing something like
json_response.Message
perhaps?
-
1
Don't
stringify
astring
, you need toparse
astring
.JSON -> object = parse
,object -> JSON = stringify
. – Matt Burland Commented Sep 8, 2015 at 13:49 - 2 possible duplicate of Parse JSON in JavaScript? – Rayon Commented Sep 8, 2015 at 13:50
- SO answer already there: stackoverflow./a/26906538/2777098 – display name Commented Sep 8, 2015 at 13:51
- You just need to Parse the json string to Javascript Object. – Mayank Commented Sep 8, 2015 at 13:57
- possible duplicate of Safely turning a JSON string into an object – Matt Burland Commented Sep 8, 2015 at 14:22
3 Answers
Reset to default 3You need to use JSON.parse()
:
var str = "{\"Message\":\"Looks like you need to login\"}";
var json = JSON.parse(str);
console.log(json.Message);
You need to use parse() method of json which is very useful. So keep using that it is very light weighted.
Here is my answer :
var myString = "{\"Message\":\"Looks like you need to login\"}";
var parsedJson = JSON.parse(myString);
alert(parsedJson.Message);
Try this:
var response = { Message: "Looks like you need to login" } ;
var json_response = JSON.stringify( response ) ;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745539808a4632075.html
评论列表(0条)