json字符串,中文Unicode编码,PHP解析问题,谢谢!

http://ip.taobao.com/instruct...

接口返回这样的数据,

{"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}}

类似这样的

"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02"

php怎么解析出来正确的汉字呢?
谢谢!

烙印99
浏览 441回答 4
4回答

元芳怎么了

function unicodeDecode($str){ $json = '{"str":"'.$str.'"}'; $arr = json_decode($json,true); if(empty($arr)) return ''; return $arr['str']; }

眼眸繁星

解决办法:首先自定义一个函数,处理接到接口的数据,将所有Unicode编码的替换为utf8, function decodeUnicode($str) { return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function( '$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");' ), $str); } PHP接口获取数据后,先执行这个函数,替换后,再执行 json_decode($str, true),这样直接可以把接口数据转化为数组了

侃侃无极

json_decode($json,true);
打开App,查看更多内容
随时随地看视频慕课网APP