JSON基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。
JSON建构于两种结构:
“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。
PHP的serialize是将变量序列化,返回一个具有变量类型和结构的字符串表达式,
说起来两者都是以一种字符串的方式来体现一种数据结构,那它们之间有什么区别呢。
先从JSON说起,看一个简单的实例。
例一:
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">var</font></strong></span><span> test = {</span><span class="string"><font color="#0000ff">"Name"</font></span><span>:</span><span class="string"><font color="#0000ff">"Peter"</font></span><span>,</span><span class="string"><font color="#0000ff">"Age"</font></span><span>:20}; </span></span></li> <li> <span>document.write(test.Name + </span><span class="string"><font color="#0000ff">": "</font></span><span> + test.Age); </span> </li> </ol>
显示结果:
Peter: 20
变量test中{"Name":"Peter","Age":20}为一个有2个元素的对象(感觉就像PHP的数组):
Name为Peter,Age为20。
当然也可以变得复杂些。
例二:
<ol class="dp-xml"> <li class="alt"><span><span>var </span><span class="attribute"><font color="#ff0000">test</font></span><span> = {"User":{"Name":"Peter","Age":20},"Company":"FORD"}; </span></span></li> <li><span>document.write(test.User.Name + ": " + test.Company); </span></li> </ol>
显示结果:
Peter: FORD这个例子中User元素中包含了Name和Age。
如果要体现多个User,则需要使用数组,区别于对象的"{}",数组使用"[]"。
JSON解析例三:
<ol class="dp-xml"> <li class="alt"><span><span>var </span><span class="attribute"><font color="#ff0000">test</font></span><span> = [ </span></span></li> <li><span> {"User":{"Name":"Peter","Age":20},"Company":"FORD"}, </span></li> <li class="alt"><span> {"User":{"Name":"Li Ming","Age":20},"Company":"Benz"} </span></li> <li><span> ]; </span></li> <li class="alt"><span>document.write(test[1].User.Name + ": " + test[1].Company); </span></li> <li><span>//或者使用:document.write(test[1]["User"]["Name"] + ": " + test[1]["Company"]); </span></li> </ol>
JSON解析显示结果:
Li Ming: Benz
通过以上简单实例就能将一些复杂数据通过一个字符串来进行传递,再配合上Ajax的确是方便很多。
下面再来看看PHP的serialize函数的作用。
JSON解析例四:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">arr</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span> </span></span></li> <li><span> ( </span></li> <li class="alt"> <span> 'Peter'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> array </span> </li> <li><span> ( </span></li> <li class="alt"> <span> 'Country'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'USA', </span> </li> <li> <span> 'Age'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>20 </span> </li> <li class="alt"><span> ), </span></li> <li> <span> 'Li Ming'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> array </span> </li> <li class="alt"><span> ( </span></li> <li> <span> 'Country'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'CHINA', </span> </li> <li class="alt"> <span> 'Age'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>21 </span> </li> <li><span> ) </span></li> <li class="alt"><span> ); </span></li> <li><span> </span></li> <li class="alt"> <span>$</span><span class="attribute-value"><font color="#0000ff">serialize</font></span><span class="attribute"><font color="#ff0000">serialize_var</font></span><span> = serialize($arr); </span> </li> <li><span>echo $serialize_var; </span></li> </ol>
JSON解析显示结果:
<ol class="dp-xml"><li class="alt"><span><span>a:2:{s:5:"Peter";a:2:{s:7:"Country";s:3:"USA";s:3:"Age";i:20;}s:7:"Li Ming";a:2:{s:7:"Country";s:5:"CHINA";s:3:"Age";i:21;}} </span></span></li></ol>
这个结果看上去比JSON要复杂一些,其实也很简单,它说明的就是一些数据类型和结构。
以a:2:{s:7:"Country";s:3:"USA";s:3:"Age";i:20;}为例:
a:2说明这是个有两个元素的数组(array),s:7:"Country";s:3:"USA";为第一个元素,s:7说明这是有7个字符的字符串(string),后面i:20;也应该猜得到是整数(integer)20。
再来看一下这个例子,
例五:
<ol class="dp-c"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">class</font></strong></span><span> test </span></span></li> <li><span>{ </span></li> <li class="alt"> <span> </span><span class="keyword"><strong><font color="#006699">var</font></strong></span><span> </span><span class="vars"><font color="#dd0000">$var</font></span><span> = 0; </span> </li> <li> <span> </span><span class="keyword"><strong><font color="#006699">function</font></strong></span><span> add(){ </span> </li> <li class="alt"> <span> </span><span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$var</font></span><span>+10; </span> </li> <li><span> } </span></li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$unserialize_var</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> test; </span> </li> <li> <span></span><span class="vars"><font color="#dd0000">$serialize_var</font></span><span> = serialize(</span><span class="vars"><font color="#dd0000">$unserialize_var</font></span><span>); </span> </li> <li class="alt"> <span></span><span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$serialize_var</font></span><span>; </span> </li> <li> <span></span><span class="vars"><font color="#dd0000">$unserialize_var</font></span><span> = null; </span> </li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$unserialize_var</font></span><span> = unserialize(</span><span class="vars"><font color="#dd0000">$serialize_var</font></span><span>); </span> </li> <li> <span></span><span class="vars"><font color="#dd0000">$unserialize_var</font></span><span>->add(); </span> </li> </ol>
显示结果:
O:4:"test":1:{s:3:"var";i:0;}
10
从这个例子中可以看出来,serialize对数据的类型和结构都进行的保存,
unserialize后的变量仍然可以使用add()方法。
那么PHP和JSON有没有联系呢,熟悉PHP的朋友应该了解PHP5.2.0已经将JSON extension设置为默认组件,也就是说我们可以在PHP中进行JSON操作,其函数为json_encode和json_decode。
例六:
<ol class="dp-c"> <li class="alt"><span><span class="vars"><font color="#dd0000">$arr</font></span><span> = </span><span class="keyword"><strong><font color="#006699">array</font></strong></span><span> </span></span></li> <li><span> ( </span></li> <li class="alt"> <span> </span><span class="string"><font color="#0000ff">'Name'</font></span><span>=></span><span class="string"><font color="#0000ff">'Peter'</font></span><span>, </span> </li> <li> <span> </span><span class="string"><font color="#0000ff">'Age'</font></span><span>=>20 </span> </li> <li class="alt"><span> ); </span></li> <li><span> </span></li> <li class="alt"> <span></span><span class="vars"><font color="#dd0000">$jsonencode</font></span><span> = json_encode(</span><span class="vars"><font color="#dd0000">$arr</font></span><span>); </span> </li> <li> <span></span><span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$jsonencode</font></span><span>; </span> </li> </ol>
显示结果:
{"Name":"Peter","Age":20}
这个结果和例一中test值是一样的,通过json_encode将PHP中的变量转换为JSON字符出表达式。
再来看看json_decode的用法。
例七:
<ol class="dp-c"> <li class="alt"><span><span class="vars"><font color="#dd0000">$var</font></span><span> = </span><span class="string"><font color="#0000ff">'{"Name":"Peter","Age":20}'</font></span><span>; </span></span></li> <li> <span></span><span class="vars"><font color="#dd0000">$jsondecode</font></span><span> = json_decode(</span><span class="vars"><font color="#dd0000">$var</font></span><span>); </span> </li> <li class="alt"> <span>print_r(</span><span class="vars"><font color="#dd0000">$jsondecode</font></span><span>); </span> </li> </ol>
显示结果:
<ol class="dp-c"><li class="alt"><span><span>stdClass Object ( [Name] => Peter [Age] => 20 ) </span></span></li></ol>
这的确验证了,在JSON中{"Name":"Peter","Age":20}是一个对象,但是在PHP中也可以将其转为数组,在json_decode中将ASSOC参数设置为True即可。
例八:
<ol class="dp-c"> <li class="alt"><span><span class="vars"><font color="#dd0000">$var</font></span><span> = </span><span class="string"><font color="#0000ff">'{"Name":"Peter","Age":20}'</font></span><span>; </span></span></li> <li> <span></span><span class="vars"><font color="#dd0000">$jsondecode</font></span><span> = json_decode(</span><span class="vars"><font color="#dd0000">$var</font></span><span>,true); </span> </li> <li class="alt"> <span>print_r(</span><span class="vars"><font color="#dd0000">$jsondecode</font></span><span>); </span> </li> </ol>
显示结果:
<ol class="dp-c"><li class="alt"><span><span>Array ( [Name] => Peter [Age] => 20 ) </span></span></li></ol>
另,需要注意的是JSON是基于Unicode格式,所以要进行中文操作要将其转化为UTF-8格式。通过上面这些例子相信大家对于JSON和PHP的serialize、json_encode都有了初步了解,结合PHP、Javascript、JSON以及Ajax就可以完成强大的数据交互功能。