为什么url编码后,然后再解码就不会出现‘\u51b0\u706b\u56fd\u5ea6’这类字符呢?
- PHP code
<?php $data = array( ‘game’ => ‘冰火国度’, ‘name’ => ‘刺之灵’, ‘country’ => ‘冰霜国’, ‘level’ => 45 ); echo json_encode( $data ); echo ‘<br>’; /** * 处理json_encode乱码 */ $newData = array(); foreach( $data as $key => $value ) { $newData[ $key ] = urlencode( $value ); } echo urldecode( json_encode( $newData ) ); ?> 结果显示: {“game”:”\u51b0\u706b\u56fd\u5ea6″,”name”:”\u523a\u4e4b\u7075″,”country”:”\u51b0\u971c\u56fd”,”level”:45} {“game”:”冰火国度”,”name”:”刺之灵”,”country”:”冰霜国”,”level”:”45″}
------解决方案--------------------
那你也没有编码之后打印啊,你编码之后立即又解码打印当然没有问题,urlencode把字节都编成普通ascii字符了,必须没有问题。