> php教程 > php手册 > php中json_encode中文编码问题

php中json_encode中文编码问题

WBOY
풀어 주다: 2016-06-14 00:01:49
원래의
837명이 탐색했습니다.

  现象:众所周知使用json_encode可以方便快捷地将对象进行json编码,但是如果对象的属性中存在着中文,问题也就随之而来了。json_encode会将中文转换为unicode编码,例如:'胥'经过json_encode处理后变为'\u80e5',最终的json中中文部分被替换为unicode编码。我们要解决的就是将对象转换为json并保证对象内部的中文在json中仍然是以正常的中文出现,现在看来只使用json_encode是不能达到目的的。

  我的解决方法:先将类中的中文字段进行url编码(urlencode),然后再对对象进行json编码(jsonencode),最后url解码(urldecode)json,即最终的json,里面的中文依旧是那个中文!

测试代码如下:

<span style="color: #008080;"> 1</span> <span style="color: #000000;"></span><span style="color: #000000;">php<br></span><span style="color: #008080;"> 2</span> <span style="color: #000000;"></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> myClass {<br></span><span style="color: #008080;"> 3</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$item1</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;"> 4</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$item2</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">中文</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;"> 5</span> <span style="color: #000000;">    <br></span><span style="color: #008080;"> 6</span> <span style="color: #000000;">    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> to_json() {<br></span><span style="color: #008080;"> 7</span> <span style="color: #000000;">        </span><span style="color: #008000;">//</span><span style="color: #008000;">url编码,避免json_encode将中文转为unicode</span><span style="color: #008000;"><br></span><span style="color: #008080;"> 8</span> <span style="color: #008000;"></span><span style="color: #000000;">        </span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">item2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #008080;">urlencode</span><span style="color: #000000;">(</span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">item2);<br></span><span style="color: #008080;"> 9</span> <span style="color: #000000;">        </span><span style="color: #800080;">$str_json</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> json_encode(</span><span style="color: #800080;">$this</span><span style="color: #000000;">);<br></span><span style="color: #008080;">10</span> <span style="color: #000000;">        </span><span style="color: #008000;">//</span><span style="color: #008000;">url解码,转完json后将各属性返回,确保对象属性不变</span><span style="color: #008000;"><br></span><span style="color: #008080;">11</span> <span style="color: #008000;"></span><span style="color: #000000;">        </span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">item2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #008080;">urldecode</span><span style="color: #000000;">(</span><span style="color: #800080;">$this</span><span style="color: #000000;">-></span><span style="color: #000000;">item2);<br></span><span style="color: #008080;">12</span> <span style="color: #000000;">        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #008080;">urldecode</span><span style="color: #000000;">(</span><span style="color: #800080;">$str_json</span><span style="color: #000000;">);<br></span><span style="color: #008080;">13</span> <span style="color: #000000;">    }<br></span><span style="color: #008080;">14</span> <span style="color: #000000;">}<br></span><span style="color: #008080;">15</span> <span style="color: #000000;"><br></span><span style="color: #008080;">16</span> <span style="color: #000000;"></span><span style="color: #800080;">$c</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> myClass();<br></span><span style="color: #008080;">17</span> <span style="color: #000000;"></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> json_encode(</span><span style="color: #800080;">$c</span><span style="color: #000000;">);<br></span><span style="color: #008080;">18</span> <span style="color: #000000;"></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;"><br></span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">19</span> <span style="color: #000000;"></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #800080;">$c</span><span style="color: #000000;">-></span><span style="color: #000000;">to_json();<br></span><span style="color: #008080;">20</span> <span style="color: #000000;"></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;"><br></span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">21</span> <span style="color: #000000;"></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> json_encode(</span><span style="color: #800080;">$c</span><span style="color: #000000;">);<br></span><span style="color: #008080;">22</span> <span style="color: #000000;"></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;"><br></span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">23</span> <span style="color: #000000;"></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> json_encode(</span><span style="color: #000000;">'</span><span style="color: #000000;">胥</span><span style="color: #000000;">'</span><span style="color: #000000;">);<br></span><span style="color: #008080;">24</span> <span style="color: #000000;"></span><span style="color: #000000;">?></span>
로그인 후 복사

程序输出结果:

<span style="color: #000000;">{</span><span style="color: #000000;">"</span><span style="color: #000000;">item1</span><span style="color: #000000;">"</span><span style="color: #000000;">:</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">item2</span><span style="color: #000000;">"</span><span style="color: #000000;">:</span><span style="color: #000000;">"</span><span style="color: #000000;">\u4e2d\u6587</span><span style="color: #000000;">"</span><span style="color: #000000;">}<br>{</span><span style="color: #000000;">"</span><span style="color: #000000;">item1</span><span style="color: #000000;">"</span><span style="color: #000000;">:</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">item2</span><span style="color: #000000;">"</span><span style="color: #000000;">:</span><span style="color: #000000;">"</span><span style="color: #000000;">中文</span><span style="color: #000000;">"</span><span style="color: #000000;">}<br>{</span><span style="color: #000000;">"</span><span style="color: #000000;">item1</span><span style="color: #000000;">"</span><span style="color: #000000;">:</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">item2</span><span style="color: #000000;">"</span><span style="color: #000000;">:</span><span style="color: #000000;">"</span><span style="color: #000000;">\u4e2d\u6587</span><span style="color: #000000;">"</span><span style="color: #000000;">}<br></span><span style="color: #000000;">"</span><span style="color: #000000;">\u80e5</span><span style="color: #000000;">"</span>
로그인 후 복사

希望本文起到抛砖引玉的作用,收集大家更好的解决方法……!

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿