無法執行的 json_encode() 
在PHP版本5.2.0之前,json_encode函式是不存在的,通常會發生這種情況最常見的原因就是所承租的主機所提供的PHP版本太舊,最近有在使用AJAX技術的人,想必一定會用到此函式,幸虧已經有高手Michal Migurski寫好這個好用的類別JSON.php,他的使用方法如下:  
 
    -   
  
    -   
  
    - include_once(\'JSON.php\');   
  
    -   
  
    -   
  
    - $json = new Services_JSON();   
  
    -   
  
    -   
  
    - $data = array(   
  
    -     123,   
  
    -     \'中文\',   
  
    -     \'key\' => \'value\',   
  
    -     array(1,2,3)   
  
    - );   
  
    -   
  
    -   
  
    - echo $json->encode($data);   
  
    -   
  
    -   
  
    -   
  
    -   
  
    -   
  
    - $jsonData = $GLOBALS[\'HTTP_RAW_POST_DATA\'];   
  
    - echo $json->decode($jsonData);  
  
 
  
//載入JSON編碼類別 
include_once(\'JSON.php\'); 
 
//建立JSON編碼物件 
$json = new Services_JSON(); 
 
//假設陣列資料 
$data = array( 
    123, 
    \'中文\', 
    \'key\' => \'value\', 
    array(1,2,3) 
); 
 
//輸出編碼後的資料 
echo $json->encode($data); 
 
//中文字碼會被自動轉換unicode的編碼 
//輸出結果:{\"0\":123,\"1\":\"\\u4e2d\\u6587\",\"key\":\"value\",\"2\":[1,2,3]} 
 
//解碼範例 
$jsonData = $GLOBALS[\'HTTP_RAW_POST_DATA\']; 
echo $json->decode($jsonData);  
你是否有發現,輸出結果的內容,是沒有斷行的,所有字串合成一行,這樣的格式並不方便閱\讀,所以我來到PHP官方挖寶,果然json_encode函式下列補充回應內容中,就有其它PHP大師,由umbrae所提供了一個將json資料格式化的函式json_format(),可將json資料編排更容易閱\讀的格式。   
 
    -   
  
    -  
  
    -  
  
    -  
  
    -  
  
    -   
  
    - function json_format($json) {   
  
    -     $tab = \"  \";   
  
    -     $new_json = \"\";   
  
    -     $indent_level = 0;   
  
    -     $in_string = false;   
  
    -     $json_obj = json_decode($json);   
  
    -     if(!$json_obj){   
  
    -         return false;   
  
    -     }   
  
    -     $json = json_encode($json_obj);   
  
    -     $len = strlen($json);   
  
    -     for($c = 0; $c < $len; $c++) {   
  
    -         $char = $json[$c];   
  
    -         switch($char) {   
  
    -             case \'{\':   
  
    -             case \'[\':   
  
    -                 if(!$in_string) {   
  
    -                     $new_json .= $char . \"\\n\" . str_repeat($tab, $indent_level+1);   
  
    -                     $indent_level++;   
  
    -                 } else {   
  
    -                     $new_json .= $char;   
  
    -                 }   
  
    -             break;   
  
    -             case \'}\':   
  
    -             case \']\':   
  
    -                 if(!$in_string){   
  
    -                     $indent_level--;   
  
    -                     $new_json .= \"\\n\" . str_repeat($tab, $indent_level) . $char;   
  
    -                 } else {   
  
    -                     $new_json .= $char;   
  
    -                 }   
  
    -             break;   
  
    -             case \',\':   
  
    -                 if(!$in_string){   
  
    -                     $new_json .= \",\\n\" . str_repeat($tab, $indent_level);   
  
    -                 } else {   
  
    -                     $new_json .= $char;   
  
    -                 }   
  
    -             break;   
  
    -             case \':\':   
  
    -                 if(!$in_string) {   
  
    -                     $new_json .= \": \";   
  
    -                 } else {   
  
    -                     $new_json .= $char;   
  
    -                 }   
  
    -             break;   
  
    -             case \'\"\':   
  
    -                 $in_string = !$in_string;   
  
    -             default:   
  
    -                 $new_json .= $char;   
  
    -             break;   
  
    -         }   
  
    -     }   
  
    -     return $new_json;   
  
    - }   
  
    -   
  
    -  
  
    -  
  
    -  
  
    -  
  
    -  
  
    -  
  
    -  
  
    -  
  
    -  
  
    -  
  
    -  
  
    -  
  
    -  
  
    -   
  
 
  
/***************************************** 
* 程式碼作者:umbrae 
* 程式碼來源:http://tw.php.net/json_encode 
* 程式碼說明:將JSON資料轉為可閱\讀排版 
******************************************/ 
function json_format($json)	{ 
    $tab = \"  \"; 
    $new_json = \"\"; 
    $indent_level = 0; 
    $in_string = false; 
    $json_obj = json_decode($json); 
    if(!$json_obj){ 
        return false; 
	} 
    $json = json_encode($json_obj); 
    $len = strlen($json); 
    for($c = 0; $c < $len; $c++) { 
        $char = $json[$c]; 
        switch($char) { 
            case \'{\': 
            case \'[\': 
                if(!$in_string) { 
                    $new_json .= $char . \"\\n\" . str_repeat($tab, $indent_level+1); 
                    $indent_level++; 
                } else { 
                    $new_json .= $char; 
                } 
            break; 
            case \'}\': 
            case \']\': 
                if(!$in_string){ 
                    $indent_level--; 
                    $new_json .= \"\\n\" . str_repeat($tab, $indent_level) . $char; 
                } else { 
                    $new_json .= $char; 
                } 
            break; 
            case \',\': 
                if(!$in_string){ 
                    $new_json .= \",\\n\" . str_repeat($tab, $indent_level); 
                } else { 
                    $new_json .= $char; 
                } 
            break; 
            case \':\': 
                if(!$in_string) { 
                    $new_json .= \": \"; 
                } else { 
                    $new_json .= $char; 
                } 
            break; 
            case \'\"\': 
                $in_string = !$in_string; 
            default: 
                $new_json .= $char; 
            break; 
        } 
    } 
    return $new_json; 
} 
 
/****************** 
* 排版後的輸出結果 
******************* 
{ 
  \"0\": 123, 
  \"1\": \"\\u4e2d\\u6587\", 
  \"key\": \"value\", 
  \"2\": [ 
    1, 
    2, 
    3 
  ] 
} 
*/  
不過在這邊特別要注意的是,json_format()中的第11行和第15行使用了json_decode()和json_encode()的函式,如果在PHP5.2.0環境下當然是正常,不過如果我們使用的是外部載入JSON.php函式,你必需將這兩行做稍微的修改(例如改為靜態用法 Services_JSON::decode和Services_JSON::encode,才行正常的使用。不過最好再另外先一個類別來使用會更為方便。例如  
 
    -   
  
    -  
  
    -  
  
    -  
  
    -  
  
    -  
  
    -   
  
    - include_once(\'JSON.php\');   
  
    - class JSON extends Services_JSON {   
  
    -       
  
    -       
  
    -       
  
    -     function encode($var,$format=false){   
  
    -         $json = parent::encode($var);   
  
    -         if($format == false){   
  
    -             return $json;   
  
    -         } else {   
  
    -             return $this->json_format($json);   
  
    -         }   
  
    -     }   
  
    -       
  
    -       
  
    -       
  
    -     function json_format($json) {   
  
    -           
  
    -           
  
    -           
  
    -     }   
  
    - }  
  
 
  
/***************************************** 
* 程式合併者:liaosankai 
* 原程式碼來源:http://tw.php.net/json_encode 
*             http://mike.teczno.com/JSON/JSON.phps 
* 程式碼說明:JSON.php強化版 
******************************************/ 
include_once(\'JSON.php\'); 
class JSON extends Services_JSON { 
    //=============== 
    //強化encode函式 
    //=============== 
    function encode($var,$format=false){ 
        $json = parent::encode($var); 
        if($format == false){ 
            return $json; 
        } else { 
            return $this->json_format($json); 
        } 
    } 
    //======================= 
    // 將JSON資料轉為可閱\讀排版 
    //======================= 
    function json_format($json)	{ 
        //程式碼省略,但請注意!! 
        //必需將json_decode($json)改為$this->decode($json); 
        //必需將json_encode($json)改為$this->enecode($json); 
    } 
}  
如此一來使用$json->encode()只要多追加一個參數,就可以決定是否要以格式化方法回傳了,你可以在此下載已改寫過的JSON.php檔,注意在建立物件時,已變成$json = new JSON()了  
注意!!最近我在使用大量資料時,發現json_format()無法運作了。後來查到主因來至於 json_format()的第15行 $json = json_encode($json_obj);,原作者似乎進行了一個多餘的動作,又將json再編碼一次,這行程式使得在進行大量資料轉換時會產生逾時的不明錯誤,故將此行拿掉,就可以正常運作了。 
  |