大鳥的學習樂園
          路漫漫其修遠兮,吾將上下而求索
          posts - 26,comments - 27,trackbacks - 0
          原文地址:http://www.homehf.com/teach/20100322/5796.html

                 由于JSON可以在很多種程序語言中使用,所以我們可以用來做小型數(shù)據(jù)中轉(zhuǎn),如:PHP輸出JSON字符串供JavaScript使用等。在PHP中可以使用 json_decode() 由一串規(guī)范的字符串解析出 JSON對象,使用 json_encode() 由JSON 對象生成一串規(guī)范的字符串。

          例:<?php

          $json = '{"a":1, "b":2, "c":3, "d":4, "e":5 }';

          var_dump(json_decode($json));

          var_dump(json_decode($json,true));

          輸出:

          object(stdClass)#1 (5) {
          ["a"] => int(1)
          ["b"] => int(2)
          ["c"] => int(3)
          ["d"] => int(4)
          ["e"] => int(5)
          }

          array(5) {
          ["a"] => int(1)
          ["b"] => int(2)
          ["c"] => int(3)
          ["d"] => int(4)
          ["e"] => int(5)
          }

          $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

          echo json_encode($arr);

          輸出:{"a":1,"b":2,"c":3,"d":4,"e":5}

          1. json_decode(),字符轉(zhuǎn)JSON,一般用在接收到Javascript 發(fā)送的數(shù)據(jù)時會用到。

          <?php
          $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}';
          $web=json_decode($s);
          echo '網(wǎng)站名稱:'.$web->webname.'<br />網(wǎng)址:'.$web->url.'<br />聯(lián)系方式:QQ-'.$web->contact->qq.'&nbsp;MAIL:'.$web->contact->mail;
          ?>

          上面的例子中,我們首先定義了一個變量s,然后用json_decode()解析成JSON對象,之后可以按照JSON的方式去使用,從使用情況看,JSON和XML以及數(shù)組實現(xiàn)的功能類似,都可以存儲一些相互之間存在關(guān)系的數(shù)據(jù),但是個人覺得JSON更容易使用,且可以使用JSON和JavaScript實現(xiàn)數(shù)據(jù)共享。

          2. json_encode(),JSON轉(zhuǎn)字符,這個一般在AJAX 應用中,為了將JSON對象轉(zhuǎn)化成字符串并輸出給 Javascript 時會用到,而向數(shù)據(jù)庫中存儲時也會用到。

          <?php
          $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}';
          $web=json_decode($s);
          echo json_encode($web);
          ?>

          二 .PHP JSON 轉(zhuǎn)數(shù)組

          <?php
          $s='{"webname":"homehf","url":"www.homehf.com","qq":"744348666"}';
          $web=json_decode($s); //將字符轉(zhuǎn)成JSON
          $arr=array();
          foreach($web as $k=>$w) $arr[$k]=$w;
          print_r($arr);
          ?>

          上面的代碼中,已經(jīng)將一個JSON對象轉(zhuǎn)成了一個數(shù)組,可是如果是嵌套的JSON,上面的代碼顯然無能為力了,那么我們寫一個函數(shù)解決嵌套JSON,


          <?php
          $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}';
          $web=json_decode($s);
          $arr=json_to_array($web);
          print_r($arr);

          function json_to_array($web){
          $arr=array();
          foreach($web as $k=>$w){
              if(is_object($w)) $arr[$k]=json_to_array($w); //判斷類型是不是object
              else $arr[$k]=$w;
          }
          return $arr;
          }
          ?>

           

          posted on 2011-03-20 18:31 大鳥 閱讀(984) 評論(0)  編輯  收藏 所屬分類: linux
          主站蜘蛛池模板: 仙游县| 昌江| 堆龙德庆县| 正蓝旗| 昌乐县| 巴林左旗| 南华县| 邹城市| 宣化县| 汤阴县| 秀山| 肇庆市| 游戏| 日喀则市| 大同市| 西丰县| 定南县| 永德县| 蓬安县| 博白县| 岑巩县| 广安市| 云浮市| 大冶市| 邵阳县| 上高县| 汪清县| 新民市| 泸西县| 荣成市| 滦南县| 隆德县| 绥江县| 濉溪县| 海晏县| 汝南县| 永德县| 沁水县| 浮梁县| 南岸区| 华池县|