Asked  7 Months ago    Answers:  5   Viewed   35 times

I receive a JSON response in an Ajax request from the server. This way it works:

{ "a" : "1", "b" : "hello 'kitty'" }

But I did not succeed in putting double quotes around kitty.

When I convert " to x22 in the Ajax response, it is still interpreted as " by JavaScript and I cannot parse the JSON.

Should I also escape the and unescape later (which would be possible)?

How to do this?

Edit: I am not sure if i expressed it well: I want this string inside of "b" after the parse:

hello "kitty"

If necessary I could also add an additional step after the parse to convert "b", but I guess it is not necessary, there is a more elegant way so this happens automatically?

Edit2: The ajax page is generated by php. I tried several things now to create the value of b, all result in JSON parse error on the page:

  $b = 'hello "kitty"';      

  // no 1:
  //$b = str_replace('"',"x22",$b);

  // or no 2:
  // $b = addslashes($b);  

  // or no 3: 
  $b = str_replace('"','"',$b);

  echo '{ "a" : "1", "b" : "' . $b . '"}';

Edit3: This solution finally works:

$b = 'hello "kitty"';      
$b = str_replace('"','\"',$b); 
echo '{ "a" : "1", "b" : "' . $b . '"}';



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"}')
  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.

Wednesday, March 31, 2021
answered 7 Months ago

Using PHP's utf8_encode() before my json_encode() did indeed stop the data from cutting off after the but it also encoded it to 092 which did not display (control character). When I used MySQL's SET NAMES utf8 before my query, I did not have to use utf8_encode() at all, and my json was encoded correctly with mapping to u2019, which displays nicely.

Thanks for the link @Pekka, it helped me narrow down the possibilities.

Wednesday, March 31, 2021
answered 7 Months ago

json_encode() has the (undocumented) habit of silently nulling properties that contain invalid (= non-UTF-8) characters.

Make sure your input data is UTF-8 encoded, which is a documented requirement of that function.

In the event of a failure to encode, json_last_error() can be used to determine the exact nature of the error. (Available in PHP 5.3 only)

Related: How to keep json_encode() from dropping strings with invalid characters

Wednesday, March 31, 2021
answered 7 Months ago

if you use single quotes, you don't need to escape double quotes, '"' is what you want.

Wednesday, March 31, 2021
answered 7 Months ago

I find the easiest way is to double up on the quotes to handle a quote.

Worksheets("Sheet1").Range("A1").Formula = "IF(Sheet1!A1=0,"""",Sheet1!A1)" 

Some people like to use CHR(34)*:

Worksheets("Sheet1").Range("A1").Formula = "IF(Sheet1!A1=0," & CHR(34) & CHR(34) & ",Sheet1!A1)" 

*Note: CHAR() is used as an Excel cell formula, e.g. writing "=CHAR(34)" in a cell, but for VBA code you use the CHR() function.

Tuesday, June 1, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :