Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 json_decode返回null空问题解决方法

json_decode返回null空问题解决方法

发布时间:2018-10-13   编辑:www.jquerycn.cn
jquery中文网为您提供json,decode返回null空问题解决方法等资源,欢迎您收藏本站,我们将为您提供最新的json,decode返回null空问题解决方法资源
json_decode对于英文估计返回都是正常的但有些朋友碰到中文时会就发现返回为null或空了,下面我来给各位朋友简单介绍此问题解决方法。

今天在做一个东西的时候需要抓取淘宝的一些数据,找到了请求的url,返回了一个callback,看了下callback中的参数是一个对象,通过正则匹配去到了数组,但是在使用json_decode()转换的时候返回的是NULL,老郁闷了,前一段时间要做一个东西也是因为这个原因,没有成功的把json对象转换成php的数组,放弃了,今天又遇到了,终于找到了解决的办法。

原因在于:抓取的数数据是是GBK格式,通过抓包看到,返回的header头中

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2526')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2526>

 Content-Type:text/html;charset=GBK

这个时候用icvo转码下,然后在json_decode()就可以正常转换了

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2885')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2885>

iconv('gbk','utf-8',$data[1][0]);


这里还需要主要的是你php代码文件的格式,建议是utf-8无bom头。


昨天发现博客的收录全部掉了,网站关了一个多月,唉。度娘你就块收了我把。

您可能感兴趣的文章:
json_decode返回null空问题解决方法
淘宝API返回的json数据无法用json_decode的问题
php中GBK/GB2312页面使用json_decode()中文丢失了解决方法
PHP Fatal error: Cannot use object of type stdClass as array in错误
PHP JSON返回null或空字符解决办法
php中curl实现GET和POST请求抓取网页或上传文件
php5.2 Json中文乱码解决方法
Jquery ajax不能解析json对象,报Invalid JSON错误的原因和解决方法
php中json_decode()和json_encode()的方法
php如何将json文本转换成数组

[关闭]