json字符串中文乱码怎么解决

如题所述

解决方法:
1、把所有的中文转为UTF-8编码(本身是utf8的话无须转)
$data="JSON中文";
$newData=iconv("GB2312","UTF-8//IGNORE",$data);
2、把中文的urlencode一下
$testJSON=array('name'=>'中文字符串','value'=>'test');
//echo json_encode($testJSON);
foreach ( $testJSON as $key => $value ) {
$testJSON[$key] = urlencode ( $value );
}
3、然后json_encode之后再urldecode一下转回来
$test_json_str = json_encode ($testJSON )
echo urldecode ($test_json_str);
查看输出结果为:
{“name”:”中文字符串”,”value”:”test”}

这样可以很好的解决中文JSON乱码问题。
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答