最近在公司做多语言商品信息同步时,由于第三方传给网站的商品信息有是json_encode之后的数据
'sizeChart' => '{"name":"\\u5973\\u88c5\\u81ea\\u5b9a\\u4e49\\u5c3a\\u7801\\u8868","unit":"cm","col_info":[{"col_name":"Notre Taille","col_number":"1","is_change":"0"},{"col_name":"Adapter \\u00e0 la Taille US","col_number":"2","is_change":"0"}网站这边做数据反转义是把斜杠去掉了,导致产品数据入库以后数据再取出来就是这种数据了:
'sizeChart' => '{"name":"u5973u88c5u81eau5b9au4e49u5c3au7801u8868","unit":"cm","col_info":[{"col_name":"Notre Taille","col_number":"1","is_change":"0"},{"col_name":"Adapter u00e0 la Taille US","col_number":"2","is_change":"0"}
1 利用 urlencode 和 urldecode 方法绕过这个转码为 unicode 的过程
header("Content-Type: text/html;charset=utf-8");
$arr = array ('lang'=>urlencode('我是中文'));
echo urldecode(json_encode($arr));
2 在PHP5.4中json_encode新增了一个选项: JSON_UNESCAPED_UNICODE, 故名思议, 就是说, json_encode不要unicode编码.
header("Content-Type: text/html;charset=utf-8");
$data = ['lang'=> '我是中文'];
echo json_encode($data,JSON_UNESCAPED_UNICODE);