Zend_Json介绍与应用

Zend_Json介绍与应用

一、Zend_Json介绍

    Zend_Json 提供一个方便的方式来串联(native的)PHP(的变量)和JSON,并将JSON(对象)解码到PHP中。 关于JSON的更多信息, 请参考 JSON 项目网站

       JSON, JavaScript Object Notation, 可以被应用于JS和其他语言的轻量级数据交换。 因为JSON(对象)可以直接被Javascript执行,对于web2.0接口来说,它是一种理想的格式;它是使用XML作为AJAX接口的一个更简单的替换方案。

       另外,Zend_Json 提供把字符串从任意的 XML 格式转换到 JSON 格式。这个内置的功能使 PHP 开发者能够在把企业数据发送到基于浏览器的 Ajax客户程序之前把它(企业数据)从 XML 格式转换到 JSON 格式。它提供了简便的方法在服务器端做动态数据转换,因而避免了在浏览器端进行不必要的 XML 解析。

二、Zend_Json应用

1.分隔符

分隔符          意义

   { }                  用于实现对象的包含,对象都包含在大括号内

     ,                   逗号用于分割对象的不同属性,或者数组的元素

    [ ]                  用于存放数组,数组将存放在中括号中

     :                   用于表示键/值对的值,冒号前为键,冒号后边就是该键的值

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);

                                                 },{

                                                            其他以此类推

                                                },

                                       ]

 

 如有不对或者不尽如人意的地方,望批评指教!

 

 

转载于:https://my.oschina.net/u/2268393/blog/349137

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值