关于PHP和JS json_encode中文处理的有关问题

WBOY
Release: 2016-06-13 12:09:13
Original
1121 people have browsed it

关于PHP和JS json_encode中文处理的问题
某个数组用JSON的格式存在数据库,使用json_encode转义。例如

[{"k":"u673au6cb9u8fc7u6ee4","v":"u4e07u516cu91ccu4e00u66f4u6362sss"},{"k":"u7a7au6c14u6ee4u82af","v":"u4e07u516cu91ccu4e00u66f4u6362"},{"k":"u71c3u6cb9u6ee4u82af","v":"u4e07u516cu91ccu4e00u66f4u6362"},{"k":"u7a7au8c03u6ee4u82af","v":"u4e07u516cu91ccu4e00u66f4u6362"},{"k":"u53d8u901fu7bb1u6cb9","v":"u4e07u516cu91ccu4e00u66f4u6362"},{"k":"u706bu82b1u585e","v":"u4e07u516cu91ccu4e00u66f4u6362"},{"k":"u5236u52a8u6db2","v":"u5e74u4e00u66f4u6362"},{"k":"u8f6cu5411u52a9u529bu6cb9","v":""},{"k":"u51b7u5374u6db2","v":""},{"k":"u6b63u65f6u94feu6761","v":""},{"k":"u76aeu5e26","v":""},{"k":"u673au6cb9u53c2u6570","v":""},{"k":"u5236u52a8u5668","v":"u89c6u68c0u67e5u7ed3u679cu800cu5b9a"},{"k":"u5907u6ce8","v":""}]
Copy after login




问题来了,HTML用AJAX获取这段代码之后,如何还原中文?
u89c6u68c0u67e5u7ed3u679cu800cu5b9a转换回中文是用什么JS函数?
------解决思路----------------------
本帖最后由 xuzuning 于 2014-11-04 19:05:54 编辑

问题是你的数据在 json_encode 后为
[{"k":"\u673a\u6cb9\u8fc7\u6ee4","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362sss"},{"k":"\u7a7a\u6c14\u6ee4\u82af","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362"},{"k":"\u71c3\u6cb9\u6ee4\u82af","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362"},{"k":"\u7a7a\u8c03\u6ee4\u82af","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362"},{"k":"\u53d8\u901f\u7bb1\u6cb9","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362"},{"k":"\u706b\u82b1\u585e","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362"},{"k":"\u5236\u52a8\u6db2","v":"\u5e74\u4e00\u66f4\u6362"},{"k":"\u8f6c\u5411\u52a9\u529b\u6cb9","v":""},{"k":"\u51b7\u5374\u6db2","v":""},{"k":"\u6b63\u65f6\u94fe\u6761","v":""},{"k":"\u76ae\u5e26","v":""},{"k":"\u673a\u6cb9\u53c2\u6570","v":""},{"k":"\u5236\u52a8\u5668","v":"\u89c6\u68c0\u67e5\u7ed3\u679c\u800c\u5b9a"},{"k":"\u5907\u6ce8","v":""}]<br />
Copy after login
为什么你要贪污掉反斜杠 \ ?
到了 html 那里不需要任何处理就可使用
<script><br />d = [{"k":"\u673a\u6cb9\u8fc7\u6ee4","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362sss"},{"k":"\u7a7a\u6c14\u6ee4\u82af","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362"},{"k":"\u71c3\u6cb9\u6ee4\u82af","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362"},{"k":"\u7a7a\u8c03\u6ee4\u82af","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362"},{"k":"\u53d8\u901f\u7bb1\u6cb9","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362"},{"k":"\u706b\u82b1\u585e","v":"\u4e07\u516c\u91cc\u4e00\u66f4\u6362"},{"k":"\u5236\u52a8\u6db2","v":"\u5e74\u4e00\u66f4\u6362"},{"k":"\u8f6c\u5411\u52a9\u529b\u6cb9","v":""},{"k":"\u51b7\u5374\u6db2","v":""},{"k":"\u6b63\u65f6\u94fe\u6761","v":""},{"k":"\u76ae\u5e26","v":""},{"k":"\u673a\u6cb9\u53c2\u6570","v":""},{"k":"\u5236\u52a8\u5668","v":"\u89c6\u68c0\u67e5\u7ed3\u679c\u800c\u5b9a"},{"k":"\u5907\u6ce8","v":""}];<br />for(i=0; i<d.length; i++)<br />  document.write(d[i].k + ' => ' + d[i].v + '<br>');<br /></script>
Copy after login
机油过滤 => 万公里一更换sss<br />空气滤芯 => 万公里一更换<br />燃油滤芯 => 万公里一更换<br />空调滤芯 => 万公里一更换<br />变速箱油 => 万公里一更换<br />火花塞 => 万公里一更换<br />制动液 => 年一更换<br />转向助力油 => <br />冷却液 => <br />正时链条 => <br />皮带 => <br />机油参数 => <br />制动器 => 视检查结果而定<br />备注 => <br />
Copy after login

------解决思路----------------------
u前面缺少斜杠了。
如果你数据库保存就是这种格式,可以这样读取。





New Document







Copy after login


机油过滤
空气滤芯
燃油滤芯
空调滤芯
变速箱油
火花塞
制动液
转向助力油
冷却液
正时链条
皮带
机油参数
制动器
备注

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template