Home > php教程 > php手册 > body text

如何正确运用PHP json_encode函数进行中文转换

WBOY
Release: 2016-06-13 11:09:53
Original
1242 people have browsed it

我们今天将要为大家带来的内容是PHP json_encode函数在处理中文转换时出现问题的解决方法。json 是一个很好的数据结构现在已经广泛用在网络数据传输上,

json_encode 和 json_decode

这两个函数的具体用法 网上有很多相关的文章 ,本文主要介绍 用json_encode 时 中文无法转换的解决方案,本文假设文件所用的编码为gb2312;

先写出所需的数组

  1.  ?PHP   
  2. $json = array (   
  3. 0 =>   
  4. array (   
  5. 'id' => '13',   
  6. 'name' => '乒乓球',   
  7. ),   
  8. 1 =>   
  9. array (   
  10. 'id' => '17',   
  11. 'name' => '篮球',   
  12. )   
  13. )   
  14. ?> 

如果直接用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>
Copy after login

结果为:

<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>
Copy after login

可以看到汉字没有被转义 都为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>
Copy after login

客户端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>
Copy after login

用上面的代码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>
Copy after login

以上就是使用PHP json_encode函数在实际操作中出现问题的解决方法。


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template