现象:众所周知使用json_encode可以方便快捷地将对象进行json编码,但是如果对象的属性中存在着中文,问题也就随之而来了。json_encode会将中文转换为unicode编码,例如:’胥’经过json_encode处理后变为’u80e5’,最终的json中中文部分被替换为unicode编码。我们要解决的就是将对象转换为json并保证对象内部的中文在json中仍然是以正常的中文出现,现在看来只使用json_encode是不能达到目的的。
我的解决方法:先将类中的中文字段进行url编码(urlencode),然后再对对象进行json编码(jsonencode),最后url解码(urldecode)json,即最终的json,里面的中文依旧是那个中文!
测试代码如下:
1 <?php
2 class myClass {
3 public $item1 = 1;
4 public $item2 = '中文';
5
6 function to_json() {
7 //url编码,避免json_encode将中文转为unicode
8 $this->item2 = urlencode($this->item2);
9 $str_json = json_encode($this);
10 //url解码,转完json后将各属性返回,确保对象属性不变
11 $this->item2 = urldecode($this->item2);
12 return urldecode($str_json);
13 }
14 }
15
16 $c = new myClass();
17 echo json_encode($c);
18 echo '<br/>';
19 echo $c->to_json();
20 echo '<br/>';
21 echo json_encode($c);
22 echo '<br/>';
23 echo json_encode('胥');
24 ?>
程序输出结果:
{"item1":1,"item2":"u4e2du6587"}
{"item1":1,"item2":"中文"}
{"item1":1,"item2":"u4e2du6587"}
"u80e5"
希望本文起到抛砖引玉的作用,收集大家更好的解决方法……!
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » 解决php中对象使用json_encode转换后中文被编码为unicode
波比源码 » 解决php中对象使用json_encode转换后中文被编码为unicode
brand levofloxacin buy levofloxacin sale
buy ampicillin 500mg cephalexin 500mg canada order erythromycin pill
indocin 75mg uk indocin 75mg us amoxicillin brand
tadalafil 5mg usa best ed drug purchase sildenafil pill
brand buspirone 10mg order ditropan 5mg online cheap ditropan order online
buy hytrin 1mg generic actos over the counter azulfidine online
purchase tacrolimus for sale ursodiol online buy urso 150mg tablet
oral imuran protonix 40mg over the counter viagra 50mg over the counter
order modafinil 100mg pill buy stromectol generic ivermectin cost in usa
order zithromax 250mg without prescription cheap zithromax 250mg neurontin 100mg ca
brand lasix lasix ca buy hydroxychloroquine 400mg
order clozaril 100mg without prescription combivent for sale dexamethasone 0,0,5 mg us
buy clomiphene 100mg for sale clomid 50mg price online gambling for real money
buy aristocort 10mg pill buy desloratadine pills desloratadine 5mg pills
tadalafil 10mg canada plavix medication buy clopidogrel 75mg
allopurinol 300mg canada order crestor sale order zetia pills
ozobax for sale buy tizanidine 2mg pill toradol price
wind creek casino online games best gambling sites casino arizona
buy tamsulosin 0.4mg without prescription flomax 0.2mg price buy generic spironolactone
cialis pills 10mg ampicillin 250mg canada ciprofloxacin 1000mg us
casino games win real money online casino real money usa purchase tadalafil online
essays buy stromectol buy online ivermectin lotion for lice
sildalis sale estrace 1mg price lamictal 200mg without prescription
buy prednisone 20mg for sale order accutane 20mg generic cheap amoxil sale
order sildenafil 50mg online cheap cheap cialis pill order cialis 20mg pills
online slots real money usa buy modafinil 100mg generic order modafinil 200mg online cheap
lasix 100mg generic order doxycycline without prescription hydroxychloroquine 200mg tablet
buy sildenafil online cheap generic tamoxifen 20mg cost budesonide