javascript - Extracting a string from a response text - Stack Overflow

I'm trying to extract just the response from the following response"{"Message":&quo

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?

Share Improve this question edited Sep 8, 2015 at 13:47 j08691 208k32 gold badges269 silver badges280 bronze badges asked Sep 8, 2015 at 13:46 BaconJuiceBaconJuice 3,77915 gold badges59 silver badges90 bronze badges 5
  • 1 Don't stringify a string, you need to parse a string. 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
Add a ment  | 

3 Answers 3

Reset to default 3

You 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

相关推荐

  • javascript - Extracting a string from a response text - Stack Overflow

    I'm trying to extract just the response from the following response"{"Message":&quo

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信