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

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

programmeradmin1浏览0评论

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 ) ;
发布评论

评论列表(0)

  1. 暂无评论