我们今天将要为大家带来的内容是PHP json_encode函数在处理中文转换时出现问题的解决方法。json 是一个很好的数据结构现在已经广泛用在网络数据传输上,
json_encode 和 json_decode
这两个函数的具体用法 网上有很多相关的文章 ,本文主要介绍 用json_encode 时 中文无法转换的解决方案,本文假设文件所用的编码为gb2312;
先写出所需的数组
如果直接用PHP json_encode函数
<ol class="dp-xml"> <li class="alt"><span class="tag"> ?PHP </span></li> <li><span>echo json_encode($json); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> <li><span> </span></li> </ol>
结果为:
<ol class="dp-xml"> <li class="alt"><span class="tag"> ?PHP </span></li> <li><span>[{"id":"13","name":null}<br>,{"id":"13","name":null}] </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> <li><span> </span></li> </ol>
可以看到汉字没有被转义 都为null,这是因为json仅仅转义encoding编码,故上面语句应该先转换编码
<ol class="dp-xml"> <li class="alt"><span class="tag"> ?PHP </span></li> <li> <span>foreach ($ajax as $</span><span class="attribute">key</span><span>=</span><span class="tag">></span><span>$val) </span> </li> <li class="alt"><span>{ </span></li> <li><span>$ajax[$key]['name'] = <br>urlencode($val['name']); </span></li> <li class="alt"><span>} </span></li> <li><span>echo json_encode($json); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
客户端js代码
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> </span><span class="tag-name">script</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/javascript"</span><span class="tag">></span><span> </span></span></span></li> <li><span>function getsort(obj) </span></li> <li class="alt"><span>{ </span></li> <li><span>$.ajax( </span></li> <li class="alt"><span>{ </span></li> <li><span>type : "GET", </span></li> <li class="alt"> <span>url : "</span><span class="tag"><span> ?=$this-</span><span class="tag">></span><span>baseUrl</span><span class="tag">?></span><span>/index/getajax", </span></span> </li> <li> <span>data : "</span><span class="attribute">c</span><span>=" obj.value, </span> </li> <li class="alt"><span>success : function(json) </span></li> <li><span>{ </span></li> <li class="alt"> <span>var </span><span class="attribute">json</span><span>=</span><span class="attribute-value">eval</span><span>(json); </span> </li> <li> <span>var </span><span class="attribute">html</span><span> = </span><span class="attribute-value">''</span><span>; </span> </li> <li class="alt"><span>$.each(json, function(k) </span></li> <li><span>{ </span></li> <li class="alt"> <span class="attribute">html</span><span> = </span><span class="attribute-value">'<span> <br>json[k]['id'] '"</span><span class="tag">></span><span>' <br>decodeURI(json[k]['name']) '</span><span class="tag"><span> /option</span><span class="tag">></span><span>'; </span></span></span> </li> <li><span>}); </span></li> <li class="alt"> <span class="attribute">html</span><span> =</span><span class="attribute-value">""</span><span>; </span> </li> <li><span>$('#sort').html(html); </span></li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"><span>) </span></li> <li><span>} </span></li> <li class="alt"><span class="tag"><span> /script</span><span class="tag">></span><span> </span></span></li> </ol>
用上面的代码js会报错 说编码不符合标准
原因是因为js 中decodeURI 仅仅支持utf8 转码。所以 ,PHP json_encode函数的代码应该为下面的代码
<ol class="dp-xml"> <li class="alt"><span class="tag"> ?PHP </span></li> <li> <span>foreach ($ajax as $</span><span class="attribute">key</span><span>=</span><span class="tag">></span><span>$val) </span> </li> <li class="alt"><span>{ </span></li> <li><span>$ajax[$key]['name'] = <br>urlencode(iconv('gb2312',<br>'utf-8',$val['name'])); </span></li> <li class="alt"><span>} </span></li> <li><span>echo json_encode($json); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> <li><span> </span></li> </ol>
以上就是使用PHP json_encode函数在实际操作中出现问题的解决方法。