搜尋
首頁php教程PHP源码PHP JSON使用實例

PHP JSON使用實例

Nov 08, 2016 pm 01:48 PM

一、json_encode()

此函數主要用來將陣列和對象,轉換為json格式。先看一個陣列轉換的例子:

   $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} 

再看一個物件轉換的例子:
   $obj->body           = 'another post';  
$obj->id             = 21;  
$obj->approved       = true;  
$obj->favorite_count = 1;  
$obj->status         = NULL;  
echo json_encode($obj);  
   结果为
{    "body":"another post",  
  "id":21,  
  "approved":true,  
  "favorite_count":1,  
  "status":null  }  


由於json只接受utf-8編碼的字符,所以json_encode()的參數必須是utf-8編碼,否則會得到空字符或null。當中文使用GB2312編碼,或外文使用ISO-8859-1編碼的時候,這一點要特別注意。

二、索引數組和關聯數組

PHP支援兩種數組,一種是只保存"值"(value)的索引數組(indexed array),另一種是保存"名值對"(name/value )的關聯數組(associative array)。

由於javascript不支援關聯數組,所以json_encode()只將索引數組(indexed array)轉為數組格式,而將關聯數組(associative array)轉為物件格式。

例如,現在有一個索引數組

   
$arr = Array('one', 'two', 'three');  
echo json_encode($arr);  

結果為: 

["one","two","three"]   

 r
{"1":"one","2":"two","3":"three"}   


注意,資料格式從"[]"(陣列)變成了"{}"(對象)。

如果你需要將"索引數組"強制轉換成"物件",可以這樣寫  

  $arr = Array('1'=>'one', '2'=>'two', '3'=>'three');
   
  echo json_encode($arr);  

或 


json_encode( (object)$arr );  

三、類(class)的轉換

json_encode ( $arr, JSON_FORCE_OBJECT );  

三、類(class)的轉換

  class Foo {  
    const     ERROR_CODE = '404';  
    public    $public_ex = 'this is public';  
    private   $private_ex = 'this is private!';  
    protected $protected_ex = 'this should be protected'; 
   
    public function getErrorCode() {  
      return self::ERROR_CODE;  
    }  
  }  


、類(class)的轉換


現在,對這個類別的實例進行json轉換: 

$foo = new Foo;  
$foo_json = json_encode($foo);  
echo $foo_json;  

輸出結果是

{"public_ex":"this is public"}      

量,除了公開變數私有變數、方法等等)都遺失了。

四、json_decode()


此函數用於將json文字轉換為對應的PHP資料結構。下面是一個例子:
   
  $json = '{"foo": 12345}';
   
  $obj = json_decode($json);  
  print $obj->{'foo'}; // 12345  
  通常情況下,json_decode()總是傳回一個PHP對象,而不是陣列。例如:
   
  $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
   
  var_dump(json_decode($json));  
   結果就是產生一個PHP物件:  

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

如果想要強制產生PHP關聯數組,json_decode()需要加一個參數true:

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

結果就產生了一個關聯數組:的常見錯誤

下面三種json寫法都是錯的,你能看出錯在哪裡嗎

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

對這三個字串執行json_decode()都會回傳null,並且報錯。


第一個的錯誤是,json的分隔符號(delimiter)只允許使用雙引號,不能使用單引號。第二個的錯誤是,json名值對的"名"(冒號左邊的部分),任何情況下都必須使用雙引號。第三個的錯誤是,最後一個值之後不能加上逗號(trailing comma)。

另外,json只能用來表示物件(object)和陣列(array),如果對一個字串或數值使用json_decode(),將會傳回null。
   var_dump(json_decode("Hello World")); //null 


🎜
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。