现象:众所周知使用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>
希望本文起到抛砖引玉的作用,收集大家更好的解决方法……!