PHP json_decode/json_encode中文内容为NULL或乱码
来源:互联网 发布:什么是网络陷阱 编辑:程序博客网 时间:2024/06/14 15:25
可能用很多朋友使用json数据时利用php自带的函数JSON_DECODE/JSON_ENCODE处理中文内容时会碰到出现NULL或乱码问题,下面我来给大家介绍为什么会出现这样的问题。
例
<?php$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';var_dump(json_decode($json));var_dump(json_decode($json, true));?>输出结果object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5)}array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5)}
完全正确没有任何问题,那么我们测试中文
代码如下 复制代码
<?php$json = '{"a":"中国人人"}';var_dump(json_decode($json));?>结果{"text":null,"status":1}
后来从php手册中得出,json_encode 和 json_decode只支持utf-8编码的字符,GBK的字符要用json就得转换一下,这样我们就好办了
转换一个编码
/** 字符串GBK转码为UTF-8,数字转换为数字。*/function ct2($s){ if(is_numeric($s)) { return intval($s); } else { return iconv("GBK","UTF-8",$s); }}/** 批量处理gbk->utf-8*/function icon_to_utf8($s) { if(is_array($s)) { foreach($s as $key => $val) { $s[$key] = icon_to_utf8($val); } } else { $s = ct2($s); } return $s;}echo json_encode(icon_to_utf8("厦门"));
这样还是有时会有问题,后来找了一种在json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来。
写了个函数
/**************************************************************** 使用特定function对数组中所有元素做处理* @param string &$array 要处理的字符串* @param string $function 要执行的函数* @return boolean $apply_to_keys_also 是否也应用到key上* @access public**************************************************************/function arrayRecursive(&$array, $function, $apply_to_keys_also = false){ foreach ($array as $key => $value) { if (is_array($value)) { arrayRecursive($array[$key], $function, $apply_to_keys_also); } else { $array[$key] = $function($value); } if ($apply_to_keys_also && is_string($key)) { $new_key = $function($key); if ($new_key != $key) { $array[$new_key] = $array[$key]; unset($array[$key]); } } }}/**************************************************************** 将数组转换为JSON字符串(兼容中文)* @param array $array 要转换的数组* @return string 转换得到的json字符串* @access public**************************************************************/function JSON($array) { arrayRecursive($array, 'urlencode', true); $json = json_encode($array); return urldecode($json);}
阅读全文
0 0
- PHP JSON_DECODE/JSON_ENCODE中文内容为NULL或乱码
- PHP json_decode/json_encode中文内容为NULL或乱码
- PHP json_decode/json_encode中文内容为NULL或乱码
- php json_encode json_decode 操作中文字符
- json_decode的结果为null,json_encode的结果为
- json_encode json_decode 乱码问题
- php json_encode 和 json_decode
- PHP json_encode中文乱码解决方法
- PHP json_encode中文乱码解决方法
- PHP json_encode中文乱码解决方法
- PHP json_encode中文乱码解决方法
- PHP json_encode中文乱码解决方法
- PHP json_encode中文乱码解决方法
- php json_encode 中文乱码解决方法
- php json_encode 中文乱码解决方法
- PHP json_encode 函数中文乱码
- php file_get_contents json_decode 输出为NULL
- php 关于json_encode/json_decode和json_encode/unserialize
- Java最小堆解决TopK问题
- 面向报文(UDP)和面向字节流(TCP)的区别
- LeeCode And Two Numbers
- PTA 求解给定字符串的前缀
- PHP json_decode/json_encode中文内容为NULL或乱码
- PHP json_decode/json_encode中文内容为NULL或乱码
- Can't create handler inside thread that has not called Looper.prepare()
- NOIP2016题解
- 知识点与实例代码
- leetcode第十六周解题总结--图
- 孤儿进程示例分析
- LNMP环境查看各个软件编译选项
- mysql安装教程
- Linux send与recv函数详解