Home >Backend Development >PHP Tutorial >json_encode()函数为什么不能解析带中文的JSON字符串呢?

json_encode()函数为什么不能解析带中文的JSON字符串呢?

WBOY
WBOYOriginal
2016-06-23 14:11:501224browse

这样就有值:

$a = '{ "a":"www", "b":2}';$arr = json_decode($a, true);


这样就返回NULL:
$a = '{ "a":"你好", "b":2}';$arr = json_decode($a, true);


为什么出现这种情况呢?该怎么解析带中文的JSON字符串呢?


回复讨论(解决方案)

json中的中文都被转码了:

$p = array("name"=>"成龙",array("age"=>55,"cur"=>"电影"));$p_json = json_encode($p);echo $p_json."<br>";#{"name":"\u6210\u9f99","0":{"age":55,"cur":"\u7535\u5f71"}}#\u6210\u9f99 => 成龙,#\u7535\u5f71 => 电影print_r(json_decode($p_json));#stdClass Object ( [name] => 成龙 [0] => stdClass Object ( [age] => 55 [cur] => 电影 ) )

json中的中文都被转码了:
PHP code

$p = array("name"=>"成龙",array("age"=>55,"cur"=>"电影"));
$p_json = json_encode($p);
echo $p_json."
";
#{"name":"\u6210\u9f99","0":{"age":55,"cur":"\u7535\u5f71"}}
#\u6210\……

那它也应该能出来东西啊,不能是NULL吧

在你的编辑器中把字符集改为utf-8

文件编码 utf-8 或者 iconv 自己再转

在你的编辑器中把字符集改为utf-8

嗯好了,谢谢啊!

不能解析中文,我设置了utf-8

header("Content-Type: text/html; charset=utf-8");$p = array("name"=>"成龙",array("age"=>55,"cur"=>"电影"));$p_json = json_encode($p);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn