您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php json_decode ,不能 转换成中文

2024/3/12 11:59:40发布21次查看
以前遇到过 将json , json_decode 后出现中文乱码 ,但至少能转换 ,这次 竟然不能转换成中文 ,
很郁闷 ,显示如下 u9633u9633u4ecau5e74u5fc5u53d1u8d22 ,编码问题还是 ……
回复内容: 以前遇到过 将json , json_decode 后出现中文乱码 ,但至少能转换 ,这次 竟然不能转换成中文 ,
很郁闷 ,显示如下 u9633u9633u4ecau5e74u5fc5u53d1u8d22 ,编码问题还是 ……
唉,我都瞧不起我自己了, 亲们,存库时,我忘了转义
//php版本5.4以上:
json_encode($data,json_unescaped_unicode);
//php版本5.2以上才有json_encode/json_decode系列函数。
//php版本5.2-5.3,可以利用php的urlencode/urldecode的组合使用,可以达到类似的保留中文的效果。
//示例如下:代码里的myjsonencode和myjsondecode函数可以保留中文。
//数组转换保留为中文的json字符串function myjsonencode($data){ return urldecode(json_encode(myurlencode($data))); //需要php版本5.4以上: //return json_encode($data,json_unescaped_unicode);}//保留中文的json字符串转换为数组function myjsondecode($data){ $data = urlencode($data); $data = str_replace(%7b,'{',$data); $data = str_replace(%7d,'}',$data); $data = str_replace(%5b,'[',$data); $data = str_replace(%5d,']',$data); $data = str_replace(%3a,':',$data); $data = str_replace(%2c,',',$data); $data = str_replace(%22,'',$data); return myurldecode(json_decode($data,true));}//自定义的url编码function myurlencode($data) { //可对关联数组进行url编码,并处理换行符 //内部递归调用 //用于myjsonencode函数调用 if(!is_array($data)){ $data = str_replace(\r,'\r',$data); $data = str_replace(\n,'\n',$data); $data = urlencode($data); } else { foreach($data as $key=>$value) { $data[myurlencode($key)] = myurlencode($value); if((string)myurlencode($key)!==(string)$key){ unset($data[$key]); } } } return $data;}//自定义的url解码function myurldecode($data) { //可对关联数组进行url解码,并处理换行符 //内部递归调用 //用于myjsondecode函数调用 if(!is_array($data)){ $data = urldecode($data); $data = str_replace('\r',\r,$data); $data = str_replace('\n',\n,$data); } else { foreach($data as $key=>$value) { $data[myurldecode($key)] = myurldecode($value); if((string)myurldecode($key)!==(string)$key){ unset($data[$key]); } } } return $data;}
//以上代码来自myphp开源函数库。
//github开源地址:https://github.com/moonlord-lm/myphp
//刷个友链。我的blog:http://www.moonlord.cn
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product