How to Put Double Quotes Inside a String Within an Ajax JSON Response from PHP

How can I put double quotes inside a string within an ajax JSON response from php?

Just escape it with a backslash:

> JSON.stringify({"a": 5, "b": 'a "kitty" mighty odd'})
{"a":5,"b":"a \"kitty\" mighty odd"}
> JSON.parse('{"a":5,"b":"a \\"kitty\\" mighty odd"}')
Object
a: 5
b: a "kitty" mighty odd
__proto__: Object

JSON parsers recognize \" inside double-quoted strings as a double quote. Note that in the second example, the double-backslash is needed because there's a Javascript parser pass, then another JSON parser pass.

Jquery.Ajax adds double quotes and new line to my JSON response

I asked one of the senior programmers where I work and he suggested I checked in hex if any hidden character was added to the response. when converting to hex in PHP, no hidden character was found, but one appeared when trying to alert in JS.

I added this code to my success function

function toHex(str) {
var hex = '';
for(var i=0;i<str.length;i++) {
hex += ''+str.charCodeAt(i).toString(16);
}
return hex;
}

alert(toHex(result));

And I noticed that the hex character "feff" were added in the new server. Looking through the internet, I found out this is the BOM char to notice the browser that the file is encoded. I had to use this small PHP script and execute it on the server to get rid of the error.

Apparently things like this happens when you open the file in notepad++ for example and save it, it can mess up the encoding.

json object becoming string because double quotes are added?

The json_encode() is working fine as it is providing the result as a JSON object as suggested by your question({ "results": "{...}" }). And JSON_OBJECT() in PDO returning a string is fine as the name JSON suggests, its JavaScript Object Notation in a humanly readable format.

You can do on the server side:

json_encode(['results'=> json_decode($query_result['results'])]);

or on client side,

function(data){ 
data.results = JSON.parse(data.results);
// Your json data here
}

Why is my server code ajax call returning a response wrapped in double-quotes?

Firstly, if you're concerned that you see ""mystring"" instead of "mystring" (double quotes instead of single quotes), that's because the developer console automatically displays quotes around string values, which can be confusing if your string actually contains the "quote" character. The outer quotes you see in the console aren't there, only the inner quotes are.

Next, according to the JSON spec (http://www.json.org/) JSON strings start and end with quotes. If you wish to parse json strings, use:

var str = JSON.parse(req.responseText);

If you simply wish to get rid of all quotes in a string, try

var str = req.responseText.replace(/\"/g, "");

Note that the latter gets rid of ALL quotes, including (escaped) quotes in the middle of the string.

If you're working with JSON objects, as your response header (application/json) seems to indicate, I strongly recommend working with JSON.parse



Related Topics



Leave a reply



Submit