I'm creating a JSON string from a PHP array. I've encoded it using json_encode()
.
我正在从PHP数组创建一个JSON字符串。我已经使用json_encode()对它进行了编码。
$data = array(
'title' => 'Example string\'s with "special" characters'
);
$data = json_encode( $data );
$data
is localized using wp_localize_script()
and is accessible via a global data
variable.
$data使用wp_localize_script()进行本地化,可以通过全局数据变量访问。
In the JS file I can access the information by the following:
在JS文件中,我可以通过以下方式获取信息:
var data = data.replace( /"/g, '"' ),
jsonData = jQuery.parseJSON( data );
console.log( jsonData );
This results in an output of:
这导致输出:
{ "title":"Example string's with "special" characters" }
Entering that result into http://jsonlint.com/ returns an error. Removing the double quotes around "special" validates the string.
将结果输入http://jsonlint.com/会返回一个错误。删除“special”周围的双引号将验证字符串。
What is the best way to create a JSON string from PHP and properly escape it for use in a JS file?
从PHP创建JSON字符串并正确转义以在JS文件中使用的最佳方式是什么?
5 个解决方案
#1
9
From http://www.php.net/manual/en/function.json-encode.php#100565
从http://www.php.net/manual/en/function.json-encode.php # 100565
That said, quotes " will produce invalid JSON, but this is only an issue if you're using json_encode() and just expect PHP to magically escape your quotes. You need to do the escaping yourself.
也就是说,引号“将产生无效的JSON,但这只是在使用json_encode()并期望PHP能够神奇地转义引号时才会出现的问题。”你需要自己逃离。
#2
19
Another way would be to encode the quotes using htmlspecialchars:
另一种方法是使用htmlspecialchars对引号进行编码:
$json_array = array(
'title' => 'Example string\'s with "special" characters'
);
$json_decode = htmlspecialchars(json_encode($json_array), ENT_QUOTES, 'UTF-8');
#3
17
I succefully just did this :
我成功地做到了:
$json = str_replace("\u0022","\\\\\"",json_encode( $phpArray,JSON_HEX_QUOT));
json_encode()
by default will escape "
to \"
. But it's still wrong JSON for json.PARSE()
. So by adding option JSON_HEX_QUOT
, json_encode()
will replace "
with \u0022
. json.PARSE()
still will not like \u0022
. So then we need to replace \u0022
with \\"
. The \\\\\"
is escaped \\"
.
json_encode()默认情况下将转义为“到\”。但是对于json.PARSE(),它仍然是错误的JSON。因此,通过添加option JSON_HEX_QUOT, json_encode()将替换为\u0022。parse()仍然不喜欢\u0022。\\\\\ \“已脱离\\”
NOTE : you can add option JSON_HEX_APOS
to replace single quote with unicode HEX
value if you have javascript single quote issue.
注意:如果存在javascript单引号问题,可以添加选项JSON_HEX_APOS,用unicode十六进制值替换单引号。
ex: json_encode( $phpArray, JSON_HEX_APOS|JSON_HEX_QUOT ));
例如:json_encode($phpArray, JSON_HEX_APOS|JSON_HEX_QUOT);
#4
14
Use json_encode($json_array, JSON_HEX_QUOT);
since php 5.3: http://php.net/manual/en/json.constants.php
使用json_encode()美元json_array JSON_HEX_QUOT);自php 5.3:http://php.net/manual/en/json.constants.php
#5
1
I just ran into this problem and the actual issue was that I forgot to add a proper application/json header before spitting out the actual JSON data.
我刚刚遇到了这个问题,实际的问题是我忘记添加一个合适的应用程序/json头,然后再输出实际的json数据。
header('Content-Type: application/json');
#1
9
From http://www.php.net/manual/en/function.json-encode.php#100565
从http://www.php.net/manual/en/function.json-encode.php # 100565
That said, quotes " will produce invalid JSON, but this is only an issue if you're using json_encode() and just expect PHP to magically escape your quotes. You need to do the escaping yourself.
也就是说,引号“将产生无效的JSON,但这只是在使用json_encode()并期望PHP能够神奇地转义引号时才会出现的问题。”你需要自己逃离。
#2
19
Another way would be to encode the quotes using htmlspecialchars:
另一种方法是使用htmlspecialchars对引号进行编码:
$json_array = array(
'title' => 'Example string\'s with "special" characters'
);
$json_decode = htmlspecialchars(json_encode($json_array), ENT_QUOTES, 'UTF-8');
#3
17
I succefully just did this :
我成功地做到了:
$json = str_replace("\u0022","\\\\\"",json_encode( $phpArray,JSON_HEX_QUOT));
json_encode()
by default will escape "
to \"
. But it's still wrong JSON for json.PARSE()
. So by adding option JSON_HEX_QUOT
, json_encode()
will replace "
with \u0022
. json.PARSE()
still will not like \u0022
. So then we need to replace \u0022
with \\"
. The \\\\\"
is escaped \\"
.
json_encode()默认情况下将转义为“到\”。但是对于json.PARSE(),它仍然是错误的JSON。因此,通过添加option JSON_HEX_QUOT, json_encode()将替换为\u0022。parse()仍然不喜欢\u0022。\\\\\ \“已脱离\\”
NOTE : you can add option JSON_HEX_APOS
to replace single quote with unicode HEX
value if you have javascript single quote issue.
注意:如果存在javascript单引号问题,可以添加选项JSON_HEX_APOS,用unicode十六进制值替换单引号。
ex: json_encode( $phpArray, JSON_HEX_APOS|JSON_HEX_QUOT ));
例如:json_encode($phpArray, JSON_HEX_APOS|JSON_HEX_QUOT);
#4
14
Use json_encode($json_array, JSON_HEX_QUOT);
since php 5.3: http://php.net/manual/en/json.constants.php
使用json_encode()美元json_array JSON_HEX_QUOT);自php 5.3:http://php.net/manual/en/json.constants.php
#5
1
I just ran into this problem and the actual issue was that I forgot to add a proper application/json header before spitting out the actual JSON data.
我刚刚遇到了这个问题,实际的问题是我忘记添加一个合适的应用程序/json头,然后再输出实际的json数据。
header('Content-Type: application/json');