php json_encode 函数:特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20694317/
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
json_encode function: special characters
提问by user2723490
Elements of an array containing special characters are converted to empty strings when encoding the array with json_encode:
使用 json_encode 对数组进行编码时,包含特殊字符的数组元素将转换为空字符串:
$arr = array ( "funds" => "ComStage STOXX?Europe 600 Techn NR ETF", "time"=>....);
$json = json_encode($arr);
After JSON encoding the element [funds] is null. It happens only with special characters (copyright, trademark etc) like the ones in "ComStage STOXX?Europe 600 Techn NR ETF".
JSON 编码后元素 [funds] 为空。它只发生在“ComStage STOXX?Europe 600 Techn NR ETF”中的特殊字符(版权、商标等)中。
Any suggestions?
有什么建议?
Thanks
谢谢
UPDATE: This is what solved the problem prior to populating the array (all names are taken from the db):
更新:这是在填充数组之前解决问题的方法(所有名称均取自数据库):
$mysqli->query("SET NAMES 'utf8'");
采纳答案by deW1
Your input has to be encoded as UTF-8 or ISO-8859-1.
您的输入必须编码为 UTF-8 或 ISO-8859-1。
http://www.php.net/manual/en/function.json-encode.php
http://www.php.net/manual/en/function.json-encode.php
Because if you try to convert an array of non-utf8 characters you'll be getting 0 as return value.
因为如果您尝试转换非 utf8 字符数组,您将获得 0 作为返回值。
Since 5.5.0 The return value on failure was changed from null string to FALSE.
自 5.5.0 起,失败时的返回值已从空字符串更改为 FALSE。
回答by Robbie Averill
The manual for json_encode specifies this:
All string data must be UTF-8 encoded.
所有字符串数据都必须采用 UTF-8 编码。
Thus, try array_mapping utf8_encode()to your array before you encode it:
因此,在编码之前尝试array_mappingutf8_encode()到您的数组:
$arr = array_map('utf8_encode', $arr);
$json = json_encode($arr);
// {"funds":"ComStage STOXX\u00c2\u00aeEurope 600 Techn NR ETF"}
For reference, take a look at the differences between the three examples on this fiddle. The first doesn't use character encoding, the second uses htmlentitiesand the third uses utf8_encode- they all return different results.
作为参考,请查看此 fiddle 上三个示例之间的差异。第一个不使用字符编码,第二个使用htmlentities和第三个使用utf8_encode- 它们都返回不同的结果。
For consistency, you should use utf8_encode().
为了保持一致性,您应该使用utf8_encode().
Docs
文档
回答by sandolkakos
To me, it works this way:
对我来说,它是这样工作的:
# Creating the ARRAY from Result.
$array=array();
while($row = $result->fetch_array(MYSQL_ASSOC))
{
# Converting each column to UTF8
$row = array_map('utf8_encode', $row);
array_push($array,$row);
}
json_encode($array);
回答by user3711086
you should use this code:
你应该使用这个代码:
$json = json_encode(array_map('utf8_encode', $arr))
array_map function converts special characters in UTF8 standard
array_map 函数转换 UTF8 标准中的特殊字符
回答by Lakin Mohapatra
Use the below function.
使用以下功能。
function utf8_converter($array)
{
array_walk_recursive($array, function (&$item, $key) {
if (!mb_detect_encoding($item, 'utf-8', true)) {
$item = utf8_encode($item);
}
});
return $array;
}
回答by user3392486
you should add charset=UTF-8 in meta tag and use json_encode for special characters
您应该在元标记中添加 charset=UTF-8 并对特殊字符使用 json_encode
$json = json_encode($arr);$json = json_encode($arr);json_encode function converts special characters in UTF8 standard
json_encode 函数转换 UTF8 标准中的特殊字符
回答by kavita
To fix the special character issue you just have to do 2 things
要解决特殊字符问题,您只需要做两件事
1.mysql_set_charset('utf8');// set this line on top of your page in which you are using json.
1. mysql_set_charset('utf8');// 将此行设置在您使用 json 的页面顶部。
- If you are saving json data in database make sure that the particular column collation is set to "
latin1_swedish_ci".
- 如果您将 json 数据保存在数据库中,请确保特定的列排序规则设置为“
latin1_swedish_ci”。

