php中json_decode返回数组或对象的实例

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. }
  14. array(5) {
  15. ["a"] => int(1)
  16. ["b"] => int(2)
  17. ["c"] => int(3)
  18. ["d"] => int(4)
  19. ["e"] => int(5)
  20. }
  21. $data='[{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""},{"Name":"a1","Number":"123","Contno":"000","QQNo":""}]';
  22. echo json_decode($data);
  23. //结果为:
  24. 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. {
  2. "from":"zh",
  3. "to":"en",
  4. "trans_result":[
  5. {
  6. "src":"u4f60u597d",
  7. "dst":"Hello"
  8. }
  9. ]
  10. }

一、json_decode返回array的方式:

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

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

我们在PHP语言中可以用以下方法取得我们想要的值,代码如下:

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

二、json_decode返回object的方式:

json_decode($data);

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

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

我们在PHP语言中可以用以下方法取得我们想要的值,代码如下:

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