国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php中json_decode返回數組或對象的實例

2024-09-04 11:46:14
字體:
來源:轉載
供稿:網友

PHP5.2.0及以上版本具有json_decode函數,該函數是用來解析JSON格式的數據,可以返回array(數組)或object(對象)兩種結果,,下面將分兩種情況具體介紹json_decode的用法以及如何取得我們想要的值.

1.json_decode()

(PHP 5 >= 5.2.0, PECL json >= 1.2.0)

json_decode — 對 JSON 格式的字符串進行編碼

說明:mixed json_decode ( string $json [, bool $assoc ] ) 

接受一個 JSON 格式的字符串并且把它轉換為 PHP 變量

參數:json 

待解碼的 json string 格式的字符串.

assoc:當該參數為 TRUE 時,將返回 array 而非 object.

返回值:Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.

范例代碼如下:

  1. <?php  
  2. $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';  
  3. var_dump(json_decode($json));  
  4. var_dump(json_decode($json, true));  
  5. ?> 
  6. //上例將輸出: 
  7. object(stdClass)#1 (5) {  
  8. ["a"] => int(1)  
  9. ["b"] => int(2)  
  10. ["c"] => int(3)  
  11. ["d"] => int(4)  
  12. ["e"] => int(5)  
  13. array(5) {  
  14. ["a"] => int(1)  
  15. ["b"] => int(2)  
  16. ["c"] => int(3)  
  17. ["d"] => int(4)  
  18. ["e"] => int(5)  
  19.  
  20. $data='[{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""}]';  
  21. echo json_decode($data); 
  22. //結果為: 
  23. Array ( [0] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => stdClass Object ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) ) 

可以看出經過json_decode()編譯出來的是對象,現在輸出json_decode($data,true)試下,代碼如下:

  1. echo json_decode($data,true); 
  2. //結果: 
  3. Array ( [0] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) ) 

可以看出 json_decode($data,true)輸出的一個關聯數組,由此可知json_decode($data)輸出的是對象,而json_decode("$arr",true)是把它強制生成PHP關聯數組.

假如我們獲取的JSON數據如下:(可以使用curl、fsockopen等方式獲取),代碼如下:

  1.  "from":"zh"
  2.  "to":"en"
  3.  "trans_result":[ 
  4.   { 
  5.    "src":"u4f60u597d"
  6.    "dst":"Hello" 
  7.   } 
  8.  ] 

一、json_decode返回array的方式:

json_decode($data,true);用json_decode函數返回array的方式得到:

  1. Array 
  2.     [from] => zh 
  3.     [to] => en 
  4.     [trans_result] => Array 
  5.         ( 
  6.             [0] => Array 
  7.                 ( 
  8.                     [src] => 你好 
  9.                     [dst] => Hello 
  10.                 ) 
  11.         ) 

我們在PHP語言中可以用以下方法取得我們想要的值,代碼如下:

  1. <?php 
  2. $data = <<<STR 
  3.  "from":"zh"
  4.  "to":"en"
  5.  "trans_result":[ 
  6.   { 
  7.    "src":"u4f60u597d"
  8.    "dst":"Hello" 
  9.   } 
  10.  ] 
  11. STR; 
  12. $jsondata=json_decode($data,true); 
  13. header("Content-Type: text/html; charset=UTF-8"); 
  14. print_r($jsondata); 
  15. echo "<br />".$jsondata['to']; //en 
  16. echo "<br />".$jsondata['trans_result'][0]['dst']; //Hello 
  17. ?> 

二、json_decode返回object的方式:

json_decode($data);

用json_decode函數返回object的方式得到:

  1. stdClass Object 
  2.     [from] => zh 
  3.     [to] => en 
  4.     [trans_result] => Array 
  5.         ( 
  6.             [0] => stdClass Object 
  7.                 ( 
  8.                     [src] => 你好 
  9.                     [dst] => Hello 
  10.                 ) 
  11.         ) 

我們在PHP語言中可以用以下方法取得我們想要的值,代碼如下:

  1. <?php 
  2. $data = <<<STR 
  3.  "from":"zh"
  4.  "to":"en"
  5.  "trans_result":[ 
  6.   { 
  7.    "src":"u4f60u597d"
  8.    "dst":"Hello" 
  9.   } 
  10.  ] 
  11. STR; 
  12. $jsondata=json_decode($data); 
  13. header("Content-Type: text/html; charset=UTF-8"); 
  14. print_r($jsondata); 
  15. echo "<br />".$jsondata->from; //zh 
  16. echo "<br />".$jsondata->trans_result[0]->src; //你好 
  17. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆昌县| 伊川县| 兴文县| 平南县| 满洲里市| 拜泉县| 互助| 巴林右旗| 隆昌县| 滦南县| 吴川市| 台南县| 芦溪县| 定安县| 乐业县| 武夷山市| 德钦县| 景谷| 天柱县| 广饶县| 郧西县| 柘荣县| 大理市| 怀化市| 武功县| 肥城市| 明水县| 临桂县| 左云县| 铜鼓县| 福安市| 沙河市| 龙井市| 泸定县| 重庆市| 云龙县| 城步| 绥江县| 读书| 望城县| 白朗县|