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

WBOY
풀어 주다: 2016-06-23 13:46:12
원래의
1058명이 탐색했습니다.

某个数组用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      
로그인 후 복사


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

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