3

My jQuery script as below:

jQuery.ajax({
    type:"GET",
    url:url, 
    data:'action=getStatus', 
}).done(function(response){ 
    var data = jQuery.parseJSON(response);

    var test = data['visitor_chart'];
})  

My jQuery.ajax return response in below form:

"{"visitor_chart":"{y: 3, label: \"2015-07-21\"}, {y: 1, label: \"2015-07-29\"}, {y: 1, label: \"2015-07-30\"}, {y: 1, label: \"2015-08-01\"}","visitor_count":6,"enquiry_count":1}"

After parseJSON I got data['visitor_chart'] in below form:

"{y: 3, label: "2015-07-21"},{y: 1, label: "2015-07-29"},{y: 1, label: "2015-07-30"},{y: 1, label: "2015-08-01"}"

but I want to strip first and last quote from this string.

How to do that?

3
  • This makes no sense, you either have valid JSON that is parsed without error, and you get an object, or you have invalid JSON that can't be parsed and just produce an error. Commented Aug 3, 2015 at 12:35
  • In my script there is no error, it works fine. But I want to use data['visitor_chart'] in an API as a argument, for this I want to remove first and last quotes. Commented Aug 3, 2015 at 12:44
  • It seems like an utterly strange result, but have you tried parsing it again, as in jQuery.parseJSON(data['visitor_chart']) Commented Aug 3, 2015 at 12:58

2 Answers 2

2

to replace first quote :

str=str.replace(/^"/, "");

to replace last quote :

str=str.replace(/"$/, "");

Verified here. It's working

Sign up to request clarification or add additional context in comments.

2 Comments

@Vinaya Maheshwari Please look at my edited answer. I have verified it.
Your example puts single quotes around the double quotes, which means you did not actually solve this issue or provide a relevant answer. I also tested your solution and it does not work.
0

you can use also replaceAll

var test = data['visitor_chart'];

console.log(test.replaceAll('"', ''));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.