php中json_decode返回數組或對象

http://www.3lian.com/edu/2014/02-11/128395.htmlphp

 

   1.json_decode()html

  json_decodejson

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

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

  說明函數

  mixed json_decode ( string $json [, bool $assoc ] )編碼

  接受一個 JSON 格式的字符串而且把它轉換爲 PHP 變量url

  參數spa

  json.net

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

  assoc

  當該參數爲 TRUE 時,將返回 array 而非 object 。

  返回值

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

  範例

  Example #1 json_decode() 的例子

 代碼以下  

<?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) 
}


$data='[{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""}]'; 
echo json_decode($data);

結果爲:

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)試下

 代碼以下  

echo json_decode($data,true);

結果:

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等方式獲取)

 代碼以下  

{
 "from":"zh",
 "to":"en",
 "trans_result":[
  {
   "src":"u4f60u597d",
   "dst":"Hello"
  }
 ]
}

  1、json_decode返回array的方式:

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

 代碼以下  

Array
(
    [from] => zh
    [to] => en
    [trans_result] => Array
        (
            [0] => Array
                (
                    [src] => 你好
                    [dst] => Hello
                )

        )

)

  咱們在PHP語言中能夠用如下方法取得咱們想要的值:

 代碼以下  

<?php
$data = <<<STR
{
 "from":"zh",
 "to":"en",
 "trans_result":[
  {
   "src":"u4f60u597d",
   "dst":"Hello"
  }
 ]
}
STR;
$jsondata=json_decode($data,true);
header("Content-Type: text/html; charset=UTF-8");
print_r($jsondata);www.111cn.net
echo "<br />".$jsondata['to']; //en
echo "<br />".$jsondata['trans_result'][0]['dst']; //Hello
?>

  2、json_decode返回object的方式:

  json_decode($data);

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

 代碼以下  

stdClass Object
(
    [from] => zh
    [to] => en
    [trans_result] => Array
        (
            [0] => stdClass Object
                (
                    [src] => 你好
                    [dst] => Hello
                )

        )

)

  咱們在PHP語言中能夠用如下方法取得咱們想要的值:

 代碼以下  

<?php
$data = <<<STR
{
 "from":"zh",
 "to":"en",
 "trans_result":[
  {
   "src":"u4f60u597d",
   "dst":"Hello"
  }
 ]
}

STR;$jsondata=json_decode($data);header("Content-Type: text/html; charset=UTF-8");print_r($jsondata);echo "<br />".$jsondata->from; //zhecho "<br />".$jsondata->trans_result[0]->src; //你好?>

相關文章
相關標籤/搜索