I use JQuery to get Json data, but the data it display has double quotes. It there a function to remove it?
$(\'div#ListingData\').text(JSON.stringify(data.
You can simple try String(); to remove the quotes.
Refer the first example here: https://www.w3schools.com/jsref/jsref_string.asp
Thank me later.
PS: TO MODs: don't mistaken me for digging the dead old question. I faced this issue today and I came across this post while searching for the answer and I'm just posting the answer.
What you are doing is making a JSON string in your example. Either don't use the JSON.stringify()
or if you ever do have JSON data coming back and you don't want quotations, Simply use JSON.parse()
to remove quotations around JSON responses! Don't use regex, there's no need to.
For niche needs when you know your data like your example ... this works :
JSON.parse(this_is_double_quoted);
JSON.parse("House"); // for example
The stringfy
method is not for parsing JSON, it's for turning an object into a JSON string.
The JSON is parsed by jQuery when you load it, you don't need to parse the data to use it. Just use the string in the data:
$('div#ListingData').text(data.data.items[0].links[1].caption);
Someone here suggested using eval()
to remove the quotes from a string. Don't do that, that's just begging for code injection.
Another way to do this that I don't see listed here is using:
let message = JSON.stringify(your_json_here); // "Hello World"
console.log(JSON.parse(message)) // Hello World
I also had this question, but in my case I didn't want to use a regex, because my JSON value may contain quotation marks. Hopefully my answer will help others in the future.
I solved this issue by using a standard string slice to remove the first and last characters. This works for me, because I used JSON.stringify()
on the textarea
that produced it and as a result, I know that I'm always going to have the "
s at each end of the string.
In this generalized example, response
is the JSON object my AJAX returns, and key
is the name of my JSON key.
response.key.slice(1, response.key.length-1)
I used it like this with a regex replace
to preserve the line breaks and write the content of that key to a paragraph block in my HTML:
$('#description').html(studyData.description.slice(1, studyData.description.length-1).replace(/\\n/g, '<br/>'));
In this case, $('#description')
is the paragraph tag I'm writing to. studyData
is my JSON object, and description
is my key with a multi-line value.