Home > Backend Development > PHP Tutorial > JSON 中文UNICODE编码有关问题,请指点

JSON 中文UNICODE编码有关问题,请指点

WBOY
Release: 2016-06-13 12:04:59
Original
1186 people have browsed it

JSON 中文UNICODE编码问题,请指点
http://bbs.csdn.net/topics/390858302
例如现在我有一串 {"a":"\u4e2d\u6587\u540d\u79f0","b":"\u6d4b\u8bd5\u7a0b\u5e8f\u3002"} 
我要用js,如何读取它的中文信息?
我要用php,如何读取它的中文信息?
请各位大牛指点迷津。谢谢。
------解决方案--------------------

<br />$content = '{"a":"\u4e2d\u6587\u540d\u79f0","b":"\u6d4b\u8bd5\u7a0b\u5e8f\u3002"}';<br />$json = json_decode($content, true);<br /><br />echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';<br />echo $json['a'].'<br>'; //中文名称<br />echo $json['b'];        // 测试程序。<br />
Copy after login


<br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><br /><html><br /> <head><br />  <meta http-equiv="content-type" content="text/html; charset=utf-8"><br />  <title> New Document </title><br /> </head><br /><br /> <body><br />  <script type="text/javascript"><br />  var content = {"a":"\u4e2d\u6587\u540d\u79f0","b":"\u6d4b\u8bd5\u7a0b\u5e8f\u3002"};<br />  document.write('a:' + content.a + ' ' + 'b:' + content.b); //a:中文名称 b:测试程序。<br />  </script><br /> </body><br /></html><br />
Copy after login

------解决方案--------------------
作为 js 代码
o = {"a":"\u4e2d\u6587\u540d\u79f0","b":"\u6d4b\u8bd5\u7a0b\u5e8f\u3002"};<br />alert(o.a) 
Copy after login
作为 js 字符串
s = '{"a":"\u4e2d\u6587\u540d\u79f0","b":"\u6d4b\u8bd5\u7a0b\u5e8f\u3002"}';<br />o = eval(s);<br />alert(o.a) 
Copy after login
按 php 对象访问
$s = '{"a":"\u4e2d\u6587\u540d\u79f0","b":"\u6d4b\u8bd5\u7a0b\u5e8f\u3002"} ';<br />$o = json_decode($s);<br />echo $o->a;
Copy after login
按 php 数组访问
$s = '{"a":"\u4e2d\u6587\u540d\u79f0","b":"\u6d4b\u8bd5\u7a0b\u5e8f\u3002"} ';<br />$a = json_decode($s, true);<br />echo $a['a'];
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