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怎么解析出来正确的汉字呢?谢谢!
4 回答
元芳怎么了
TA贡献1798条经验 获得超7个赞
function unicodeDecode($str){
$json = '{"str":"'.$str.'"}';
$arr = json_decode($json,true);
if(empty($arr)) return '';
return $arr['str'];
}
眼眸繁星
TA贡献1873条经验 获得超9个赞
解决办法:
首先自定义一个函数,处理接到接口的数据,将所有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),这样直接可以把接口数据转化为数组了
- 4 回答
- 0 关注
- 440 浏览
添加回答
举报
0/150
提交
取消