Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP json_encode()UTF-8特殊字符失败_Php_Json_Encoding_Utf 8 - Fatal编程技术网

PHP json_encode()UTF-8特殊字符失败

PHP json_encode()UTF-8特殊字符失败,php,json,encoding,utf-8,Php,Json,Encoding,Utf 8,我使用html_entity_decode($string)来解码html特殊字符,例如ä=ä。然后,我使用json_encode()创建一个json字符串,用于Android应用程序。我的问题是我得到的输出是\u00e4而不是'a'。我知道json_encode()只适用于UTF-8编码的字符串,但当我对我的值运行mb_detect_encding($myString)时,它会返回“UTF-8”。对这些值运行ut8_encode()没有帮助。这是我的密码: $newsList = ar

我使用html_entity_decode($string)来解码html特殊字符,例如ä=ä。然后,我使用json_encode()创建一个json字符串,用于Android应用程序。我的问题是我得到的输出是\u00e4而不是'a'。我知道json_encode()只适用于UTF-8编码的字符串,但当我对我的值运行mb_detect_encding($myString)时,它会返回“UTF-8”。对这些值运行ut8_encode()没有帮助。这是我的密码:

$newsList = array();
while($row = $news->fetch_object()){
  $tmpNews = new News();
  $tmpNews->imgId = $row->image_id;
  $tmpNews->author = html_entity_decode($row->author);
  $tmpNews->subject = $row->subject;
  $tmpNews->msg = $row->msg;
  $tmpNews->newsmsg = $row->newsmsg;
  $tmpNews->date = $row->wdate;
  array_push($newsList, $tmpNews);
  $tmpNews = null;
}

$json = array();
foreach($newsList as $news){
  array_push($json, $news->getJson());
}
var_dump($json);
echo json_encode($json);

当我执行var_dump($json)时,我的特殊字符会正常显示

尝试json_encode($json,true);您能否将变量名“$json”更改为“$array”,因为当名称不反映内容时,可能会让人感到困惑。然后将最后一行从“echo json_encode($json);”更改为“$json=json_encode($json);”。最后,var_转储$array和$json,并将输出发布到此处:)json_encode($json,true);没帮上忙。可以补充的是,添加标志JSON_UNESCAPED_UNICODE有助于解决问题。但是我使用的服务器运行的是PHP5.3.3,因此我不能使用该标志。