Home  >  Article  >  Backend Development  >  关于PHP和JS json_encode中文处理的问题

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

WBOY
WBOYOriginal
2016-06-23 13:46:121058browse

某个数组用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":""}]




问题来了,HTML用AJAX获取这段代码之后,如何还原中文?
u89c6u68c0u67e5u7ed3u679cu800cu5b9a转换回中文是用什么JS函数?


回复讨论(解决方案)

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

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

      New Document      


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

Statement:
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