如何在php ajax JSON响应的字符串中放入双引号?

时间:2022-09-15 13:02:30

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

我从服务器收到一个Ajax请求中的JSON响应。这种方式工作原理:

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

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

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

但是我没有在kitty周围加上双引号。

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

当我在Ajax响应中转换成“\x22”时,它仍然被解释为“通过JavaScript,我无法解析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:

编辑:我不确定我是否表达得很好:我想在解析后把这个字符串放在“b”中:

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?

如果需要的话,我也可以在解析之后添加一个额外的步骤来转换“b”,但是我想这是不必要的,有一种更优雅的方式可以自动执行?

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:

ajax页面是由php生成的。我现在尝试了一些方法来创建b的值,所有这些都导致页面上的JSON解析错误:

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

3 个解决方案

#1


56  

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.

JSON解析器识别\“在双引号内的字符串为双引号”。注意,在第二个示例中,需要双反斜杠,因为有一个Javascript解析器传递,然后有另一个JSON解析器传递。

#2


2  

use just json_encode (any PHP element ), it will automatically parses.

使用json_encode(任何PHP元素),它将自动解析。

#3


0  

A little off-topic, you could use JavaScript/NodeJS on your server and use ES6 template literals (the backticks `` used around "Christian"), but 7 years later you probably already use NodeJS :)

稍微离题一点,您可以在服务器上使用JavaScript/NodeJS,并使用ES6模板文字(在“Christian”前后使用“backtick”),但7年后,您可能已经使用NodeJS:)

var myJSON = {
    "name": {
        "first": `"Christian"`,
        "last": "Broberg"
    },
    "age": 49,
    "skills": [ "JavaScript", "React", "NodeJS" ],
    "married": false,
    "superpowers": null
}

#1


56  

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.

JSON解析器识别\“在双引号内的字符串为双引号”。注意,在第二个示例中,需要双反斜杠,因为有一个Javascript解析器传递,然后有另一个JSON解析器传递。

#2


2  

use just json_encode (any PHP element ), it will automatically parses.

使用json_encode(任何PHP元素),它将自动解析。

#3


0  

A little off-topic, you could use JavaScript/NodeJS on your server and use ES6 template literals (the backticks `` used around "Christian"), but 7 years later you probably already use NodeJS :)

稍微离题一点,您可以在服务器上使用JavaScript/NodeJS,并使用ES6模板文字(在“Christian”前后使用“backtick”),但7年后,您可能已经使用NodeJS:)

var myJSON = {
    "name": {
        "first": `"Christian"`,
        "last": "Broberg"
    },
    "age": 49,
    "skills": [ "JavaScript", "React", "NodeJS" ],
    "married": false,
    "superpowers": null
}