Heim > Artikel > Backend-Entwicklung > 在PHP语言中使用JSON_PHP教程
目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它。
我写过一篇《数据类型和JSON格式》,探讨它的设计思想。今天,我想总结一下PHP语言对它的支持,这是开发互联网应用程序(特别是编写API)必须了解的知识。
从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码。
一、json_encode()
该函数主要用来将数组和对象,转换为json格式。先看一个数组转换的例子:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$arr</span><span> = </span><span class="keyword">array</span><span> (</span><span class="string">'a'</span><span>=>1,</span><span class="string">'b'</span><span>=>2,</span><span class="string">'c'</span><span>=>3,</span><span class="string">'d'</span><span>=>4,</span><span class="string">'e'</span><span>=>5); </span></span></li> <li> <span class="func">echo</span><span> json_encode(</span><span class="vars">$arr</span><span>); </span> </li> </ol>
结果为
<ol class="dp-c"><li class="alt"><span><span>{</span><span class="string">"a"</span><span>:1,</span><span class="string">"b"</span><span>:2,</span><span class="string">"c"</span><span>:3,</span><span class="string">"d"</span><span>:4,</span><span class="string">"e"</span><span>:5} </span></span></li></ol>
再看一个对象转换的例子:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$obj</span><span>->body = </span><span class="string">'another post'</span><span>; </span></span></li> <li> <span class="vars">$obj</span><span>->id = 21; </span> </li> <li class="alt"> <span class="vars">$obj</span><span>->approved = true; </span> </li> <li> <span class="vars">$obj</span><span>->favorite_count = 1; </span> </li> <li class="alt"> <span class="vars">$obj</span><span>->status = NULL; </span> </li> <li> <span class="func">echo</span><span> json_encode(</span><span class="vars">$obj</span><span>); </span> </li> </ol>
结果为
<ol class="dp-c"> <li class="alt"><span><span>{ </span></span></li> <li> <span class="string">"body"</span><span>:</span><span class="string">"another post"</span><span>, </span> </li> <li class="alt"> <span class="string">"id"</span><span>:21, </span> </li> <li> <span class="string">"approved"</span><span>:true, </span> </li> <li class="alt"> <span class="string">"favorite_count"</span><span>:1, </span> </li> <li> <span class="string">"status"</span><span>:null </span> </li> <li class="alt"><span>} </span></li> </ol>
由于json只接受utf-8编码的字符,所以json_encode()的参数必须是utf-8编码,否则会得到空字符或者null。当中文使用GB2312编码,或者外文使用ISO-8859-1编码的时候,这一点要特别注意。
二、索引数组和关联数组
PHP支持两种数组,一种是只保存"值"(value)的索引数组(indexed array),另一种是保存"名值对"(name/value)的关联数组(associative array)。
由于javascript不支持关联数组,所以json_encode()只将索引数组(indexed array)转为数组格式,而将关联数组(associative array)转为对象格式。
比如,现在有一个索引数组
<ol class="dp-c"> <li class="alt"><span><span class="vars">$arr</span><span> = Array(</span><span class="string">'one'</span><span>, </span><span class="string">'two'</span><span>, </span><span class="string">'three'</span><span>); </span></span></li> <li> <span class="func">echo</span><span> json_encode(</span><span class="vars">$arr</span><span>); </span> </li> </ol>
结果为:
<ol class="dp-c"><li class="alt"><span><span>[</span><span class="string">"one"</span><span>,</span><span class="string">"two"</span><span>,</span><span class="string">"three"</span><span>] </span></span></li></ol>
如果将它改为关联数组:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$arr</span><span> = Array(</span><span class="string">'1'</span><span>=></span><span class="string">'one'</span><span>, </span><span class="string">'2'</span><span>=></span><span class="string">'two'</span><span>, </span><span class="string">'3'</span><span>=></span><span class="string">'three'</span><span>); </span></span></li> <li> <span class="func">echo</span><span> json_encode(</span><span class="vars">$arr</span><span>); </span> </li> </ol>
结果就变了:
<ol class="dp-c"><li class="alt"><span><span>{</span><span class="string">"1"</span><span>:</span><span class="string">"one"</span><span>,</span><span class="string">"2"</span><span>:</span><span class="string">"two"</span><span>,</span><span class="string">"3"</span><span>:</span><span class="string">"three"</span><span>} </span></span></li></ol>
注意,数据格式从"[]"(数组)变成了"{}"(对象)。
如果你需要将"索引数组"强制转化成"对象",可以这样写
<ol class="dp-c"><li class="alt"><span><span>json_encode( (object)</span><span class="vars">$arr</span><span> ); </span></span></li></ol>
或者
<ol class="dp-c"><li class="alt"><span><span>json_encode ( </span><span class="vars">$arr</span><span>, JSON_FORCE_OBJECT ); </span></span></li></ol>
三、类(class)的转换
下面是一个PHP的类:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">class</span><span> Foo { </span></span></li> <li> <span class="keyword">const</span><span> ERROR_CODE = </span><span class="string">'404'</span><span>; </span> </li> <li class="alt"> <span class="keyword">public</span><span> </span><span class="vars">$public_ex</span><span> = </span><span class="string">'this is public'</span><span>; </span> </li> <li> <span class="keyword">private</span><span> </span><span class="vars">$private_ex</span><span> = </span><span class="string">'this is private!'</span><span>; </span> </li> <li class="alt"> <span class="keyword">protected</span><span> </span><span class="vars">$protected_ex</span><span> = </span><span class="string">'this should be protected'</span><span>; </span> </li> <li> <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> getErrorCode() { </span> </li> <li class="alt"> <span class="keyword">return</span><span> self::ERROR_CODE; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> </ol>
现在,对这个类的实例进行json转换:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$foo</span><span> = </span><span class="keyword">new</span><span> Foo; </span></span></li> <li> <span class="vars">$foo_json</span><span> = json_encode(</span><span class="vars">$foo</span><span>); </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="vars">$foo_json</span><span>; </span> </li> </ol>
输出结果是
<ol class="dp-c"><li class="alt"><span><span>{</span><span class="string">"public_ex"</span><span>:</span><span class="string">"this is public"</span><span>} </span></span></li></ol>
可以看到,除了公开变量(public),其他东西(常量、私有变量、方法等等)都遗失了。
四、json_decode()
该函数用于将json文本转换为相应的PHP数据结构。下面是一个例子:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$json</span><span> = </span><span class="string">'{"foo": 12345}'</span><span>; </span></span></li> <li> <span class="vars">$obj</span><span> = json_decode(</span><span class="vars">$json</span><span>); </span> </li> <li class="alt"> <span>print </span><span class="vars">$obj</span><span>->{</span><span class="string">'foo'</span><span>}; </span><span class="comment">// 12345</span><span> </span> </li> </ol>
通常情况下,json_decode()总是返回一个PHP对象,而不是数组。比如:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$json</span><span> = </span><span class="string">'{"a":1,"b":2,"c":3,"d":4,"e":5}'</span><span>; </span></span></li> <li> <span>var_dump(json_decode(</span><span class="vars">$json</span><span>)); </span> </li> </ol>
结果就是生成一个PHP对象:
<ol class="dp-c"> <li class="alt"><span><span>object(stdClass)#1 (5) { </span></span></li> <li> <span>[</span><span class="string">"a"</span><span>] => int(1) </span> </li> <li class="alt"> <span>[</span><span class="string">"b"</span><span>] => int(2) </span> </li> <li> <span>[</span><span class="string">"c"</span><span>] => int(3) </span> </li> <li class="alt"> <span>[</span><span class="string">"d"</span><span>] => int(4) </span> </li> <li> <span>[</span><span class="string">"e"</span><span>] => int(5) </span> </li> <li class="alt"><span>} </span></li> </ol>
如果想要强制生成PHP关联数组,json_decode()需要加一个参数true:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$json</span><span> = </span><span class="string">'{"a":1,"b":2,"c":3,"d":4,"e":5}'</span><span>; </span></span></li> <li> <span>var_dump(json_decode(</span><span class="vars">$json</span><span>),true); </span> </li> </ol>
结果就生成了一个关联数组:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">array</span><span>(5) { </span></span></li> <li> <span>[</span><span class="string">"a"</span><span>] => int(1) </span> </li> <li class="alt"> <span>[</span><span class="string">"b"</span><span>] => int(2) </span> </li> <li> <span>[</span><span class="string">"c"</span><span>] => int(3) </span> </li> <li class="alt"> <span>[</span><span class="string">"d"</span><span>] => int(4) </span> </li> <li> <span>[</span><span class="string">"e"</span><span>] => int(5) </span> </li> <li class="alt"><span>} </span></li> </ol>
五、json_decode()的常见错误
下面三种json写法都是错的,你能看出错在哪里吗?
<ol class="dp-c"> <li class="alt"><span><span class="vars">$bad_json</span><span> = </span><span class="string">"{ 'bar': 'baz' }"</span><span>; </span></span></li> <li> <span class="vars">$bad_json</span><span> = </span><span class="string">'{ bar: "baz" }'</span><span>; </span> </li> <li class="alt"> <span class="vars">$bad_json</span><span> = </span><span class="string">'{ "bar": "baz", }'</span><span>; </span> </li> </ol>
对这三个字符串执行json_decode()都将返回null,并且报错。
第一个的错误是,json的分隔符(delimiter)只允许使用双引号,不能使用单引号。第二个的错误是,json名值对的"名"(冒号左边的部分),任何情况下都必须使用双引号。第三个的错误是,最后一个值之后不能添加逗号(trailing comma)。
另外,json只能用来表示对象(object)和数组(array),如果对一个字符串或数值使用json_decode(),将会返回null。
<ol class="dp-c"><li class="alt"><span><span>var_dump(json_decode(</span><span class="string">"Hello World"</span><span>)); </span><span class="comment">//null</span><span> </span></span></li></ol>
六、参考材料
* PHP Manual (http://php.net/manual/en/book.json.php)
* Ed Finkler, JSON is Everybody's Friend (http://phpadvent.org/2008/json-is-everybodys-friend-by-ed-finkler)
原文地址:http://www.ruanyifeng.com/blog/2011/01/json_in_php.html