Zend_Json介绍与应用php
1、Zend_Json介绍web
Zend_Json
提供一个方便的方式来串联(native的)PHP(的变量)和JSON,并将JSON(对象)解码到PHP中。 关于JSON的更多信息, 请参考 JSON 项目网站。json
JSON, JavaScript Object Notation, 能够被应用于JS和其余语言的轻量级数据交换。 由于JSON(对象)能够直接被Javascript执行,对于web2.0接口来讲,它是一种理想的格式;它是使用XML做为AJAX接口的一个更简单的替换方案。数组
另外,Zend_Json
提供把字符串从任意的 XML 格式转换到 JSON 格式。这个内置的功能使 PHP 开发者可以在把企业数据发送到基于浏览器的 Ajax客户程序以前把它(企业数据)从 XML 格式转换到 JSON 格式。它提供了简便的方法在服务器端作动态数据转换,于是避免了在浏览器端进行没必要要的 XML 解析。浏览器
2、Zend_Json应用服务器
1.分隔符网站
分隔符 意义ui
{ } 用于实现对象的包含,对象都包含在大括号内spa
, 逗号用于分割对象的不一样属性,或者数组的元素code
[ ] 用于存放数组,数组将存放在中括号中
: 用于表示键/值对的值,冒号前为键,冒号后边就是该键的值
2.基本方法
2.1 普通数组 转化成 json $json=Zend_Json::encode($array);
2.2. json 转化成 普通数组 $array=Zend_Json::decode($json);
2.3 json 转化成 对象类型 $stdClass=Zend_Json::decode($json,Zend_Json::TYPE_OBJECT);
2.4 XML 转化成 json $json=Zend_Json::fromXml($xml)
3. 示例
3.1 将普通数组转化为json
示例代码以下:
require_once 'Zend/Json.php'; $temp=array( "a" => 0, "b" => 1, "c" =>array( "c-1" => 21, "c-2" => 22, "c-3" => 23 ), "d" => 3 ); $json=Zend_Json::encode($temp); echo "临时数组内容为: "; print_r($temp); echo "<br/>"; echo "转换为json格式内容为:"; print_r($json);
输出结果为:
临时数组内容为: Array ( [a] => 0 [b] => 1 [c] => Array ( [c-1] => 21 [c-2] => 22 [c-3] => 23 ) [d] => 3 [e] => 4 )
转换为json格式内容为:{"a":0,"b":1,"c":{"c-1":21,"c-2":22,"c-3":23},"d":3,"e":4}
3.2 将json转化为普通数组
示例代码以下:
require_once 'Zend/Json.php'; $json='{"a":0,"b":1,"c":{"c-1":21,"c-2":22,"c-3":23},"d":3,"e":4}'; $temp=Zend_Json::decode($json); echo "临时json内容为:"; print_r($json); echo "<br/>"; echo "转换为普通数组格式内容为:"; print_r($temp);
结果为:
临时json内容为: {"a":0,"b":1,"c":{"c-1":21,"c-2":22,"c-3":23},"d":3,"e":4}
转换为普通数组格式内容为:Array ( [a] => 0 [b] => 1 [c] => Array ( [c-1] => 21 [c-2] => 22 [c-3] => 23 ) [d] => 3 [e] => 4 )
3.3 将json转化为对象
承接上述例子,在示例代码中输入如下代码:
echo "<br/>输出将json解码后的对象为 :"; $vative=Zend_Json::decode($json,Zend_Json::TYPE_OBJECT); print_r($vative);
输出结果为:
输出将json解码后的对象为:stdClass Object ( [a] => 0 [b] => 1 [c] => stdClass Object ( [c-1] => 21 [c-2] => 22 [c-3] => 23 ) [d] => 3 [e] => 4 );
3.4 xml转化为json(源自:zend_framework手册)
xml代码以下:
<?xml version="1.0" encoding="UTF-8"?> <books> <book id="1"> <title>Code Generation in Action</title> <author><first>Jack</first><last>Herrington</last></author> <publisher>Manning</publisher> </book> <book id="2"> <title>PHP Hacks</title> <author><first>Jack</first><last>Herrington</last></author> <publisher>O'Reilly</publisher> </book> <book id="3"> <title>Podcasting Hacks</title> <author><first>Jack</first><last>Herrington</last></author> <publisher>O'Reilly</publisher> </book> </books>
转化为json,代码以下:
{ "books" : { "book" : [ { "@attributes" : { "id" : "1" }, "title" : "Code Generation in Action", "author" : { "first" : "Jack", "last" : "Herrington" }, "publisher" : "Manning" }, { "@attributes" : { "id" : "2" }, "title" : "PHP Hacks", "author" : { "first" : "Jack", "last" : "Herrington" }, "publisher" : "O'Reilly" }, { "@attributes" : { "id" : "3" }, "title" : "Podcasting Hacks", "author" : { "first" : "Jack", "last" : "Herrington" }, "publisher" : "O'Reilly" } ] } } ?>
1. 对xml解析时,将属性同一放在“@attriutes”下,如
xml代码: <book id="2" name="sw" ...> .. </book>
解析后 json代码: "book" : {
"@attributes" : {
"id"=>"2", "name"=>"sw", more(更多属性)
},
.......
},
2. 若是名称相同,则归为一个数组,如上例:
xml代码: <book id="1"> ... </book>
<book id="2" > .... </book>
解析后json代码:(book是个数组形式)
“book" : [
{
这里是第一个book(id=1);
},{
这里是第二个book(id=2);
},{
其余以此类推
},
]
若有不对或者不尽如人意的地方,望批评指教!