如何在来自 php 的 ajax JSON 响应中的字符串中放置双引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2732409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I put double quotes inside a string within an ajax JSON response from php?
提问by user89021
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”:“你好'小猫'”}
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)?
我是否也应该稍后转义 \ 和 unescape (这是可能的)?
How to do this?
这该怎么做?
Edit:I am not sure if i expressed it well: I want this string inside of "b" afterthe 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:
Edit2: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:
Edit3:这个解决方案终于奏效了:
$b = 'hello "kitty"';
$b = str_replace('"','\"',$b);
echo '{ "a" : "1", "b" : "' . $b . '"}';
回答by Max Shawabkeh
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 解析器传递。
回答by mahesh
use just json_encode (any PHP element ), it will automatically parses.
只使用 json_encode(任何 PHP 元素),它会自动解析。
回答by Christian Broberg
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”周围使用的反引号``),但是 7 年后你可能已经使用了 NodeJS :)
var myJSON = {
"name": {
"first": `"Christian"`,
"last": "Broberg"
},
"age": 49,
"skills": [ "JavaScript", "React", "NodeJS" ],
"married": false,
"superpowers": null
}

