php中json_decode返回数组或对象

简介: http://www.3lian.com/edu/2014/02-11/128395.html      1.json_decode()   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 格式的字符串。

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

 

   1.json_decode()

  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.

  范例

  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"
  }
 ]
}

  一、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
?>

  二、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; //zh
echo "<br />".$jsondata->trans_result[0]->src; //你好
?>

 

目录
相关文章
|
1月前
|
Java 程序员 PHP
PHP对象和类
PHP对象和类
21 0
|
2月前
|
Web App开发 JSON JavaScript
SAP UI5 应用程序小技巧 - 一键将 JSON 对象导出成本地 json 文件
SAP UI5 应用程序小技巧 - 一键将 JSON 对象导出成本地 json 文件
25 0
|
3月前
|
JSON PHP 数据格式
|
1月前
|
存储 JSON 安全
【C++ JSON库 json值的创建手段】深入探究C++中JSON对象定位与操作:从引用到回调函数
【C++ JSON库 json值的创建手段】深入探究C++中JSON对象定位与操作:从引用到回调函数
66 0
|
23天前
|
JSON C语言 数据格式
C语言与lua通过json对象交互
C语言与lua通过json对象交互
17 1
|
24天前
|
JSON 数据格式
Json字符串与QVariantList 对象相互转换
Json字符串与QVariantList 对象相互转换
7 0
|
1月前
|
存储 JSON JavaScript
Python如何解析json对象?
Python如何解析json对象?
25 1
|
1月前
|
JSON JavaScript PHP
PHP把unicode编码的json字符串转中文
PHP把unicode编码的json字符串转中文
13 0
|
2月前
|
SQL JSON Apache
Flink问题之嵌套 json 中string 数组的解析异常如何解决
Apache Flink是由Apache软件基金会开发的开源流处理框架,其核心是用Java和Scala编写的分布式流数据流引擎。本合集提供有关Apache Flink相关技术、使用技巧和最佳实践的资源。
222 1
|
2月前
|
存储 JSON 前端开发
让你的对象变得拗口:JSON.stringify(),我把对象夹进了 JSON 魔法帽!
在 JavaScript 中,JSON.stringify() 是一个内置函数,用于将 JavaScript 对象转换为 JSON 字符串。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,广泛用于前后端数据传输和存储。本文将详细介绍 JSON.stringify() 的属性、应用场景,并提供一个完整而优雅的实现,处理循环引用、特殊类型(如日期和正则表达式)以及性能相关的问题。同时,我们还将讨论注意事项和相关引用资料。