json_encode() 和 json_decode()
var obj={"a":"v","b":"x"}; //这表示变量obj是一个对象,它有两个属性:a和b,属性值分别是:v和x.
var arr=["v","x"]; //这表示变量arr是一个数组,它有两一元素,索引分别是0和1,值分别是:v和x.
例1:
<?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)
}*/
?>
例2:
<?php
$json_string='{"id":1,"name":"mike","country":"usa","office":["microsoft","oracle"]} ';
$obj=json_decode($json_string);
//可以使用以下的方法来输出和访问
echo $obj->name; //displays mike
echo $obj->office[0]; //displays microsoft
?>
注意:
(1)使用UTF-8编码
(2)不能在最后元素有逗号
(3)不能使用单引号
(4)不能有\r,\t,如果有请替换
$json='{"item1":{"item11":{"n":"chenling","m":"llll"},"sex":"男","age":"25"},"item2":{"item21":"ling","sex":"女","age":"24"}}';
$J=json_decode($json);
print_r($J);
/*stdClass Object(
[item1] => stdClass Object(
[item11] => stdClass Object(
[n] => chenling
[m] => llll
)
[sex] => 男
[age] => 25
)
[item2] => stdClass Object(
[item21] => ling
[sex] => 女
[age] => 24
)
)*/
要取得了值是chenling的那个属性,则应该这样访问:$J->item1->item11->n; //这将取得属性n的值:chenling
$json='{"item1":[{"name":[{"chen":"chenling","ling":"chenli"}],"sex":"男","age":"25"}{"name":"sun","sex":"女","age":"24"}]}';
$J=json_decode($json);
print_r($J);
/*stdClass Object(
[item1] => Array(
[0] => stdClass Object(
[name] => Array(
[0] => stdClass Object(
[chen] => chenling
[ling] => chenli
)
)
[sex] => 男
[age] => 25
)
[1] => stdClass Object(
[name] => sun
[sex] => 女
[age] => 24
)
)
)*/
$json='[["item1","item11"],["n","chenling"],["m","llll"]]';
$J=json_decode($json);
print_r($J);
/*Array(
[0] => Array(
[0] => item1
[1] => item11
)
[1] => Array(
[0] => n
[1] => chenling
)
[2] => Array(
[0] => m
[1] => llll
)
)*/
要取得了值是chenling的那个元素,则应该这样访问: $J[0][1];//这将取得元素值chenling的那个元素,但是用这种方式有一个缺点,就是无法用字符串作为索引,只能用数字,用完全对象的形式可以解决这个问题,其实这种访问形式就是数组的访问方式,相当于访问一个2维数组。
json_encode() 和 json_decode()的更多相关文章
- php中的json_encode()和json_decode()函数的一些说明
一,json语法( php中的json_decode($json)中的$json要符合json语法格式 ) ① JSON可以表示三种类型的值 1,简单值.包括整型,字符串型,布尔值和null.例如:5 ...
- php json_encode()和json_decode()
json_encode()和json_decode()分别是编译和反编译过程 注意json只接受utf-8编码的字符,所以json_encode()的参数必须是utf-8编码,否则会得到空字符或者nu ...
- PHP数组和Json之间的互相转换 json_encode() 和 json_decode()
之所以要用到Json,很多时候是因为使用ajax对象时,程序与JS函数之间的数据交互.因为JS不认识PHP中的数组,PHP也不认识JS中的数组或对象.Json很好的解决了这个问题. Json简介 JS ...
- json_encode和json_decode和isset和array_key_exists
1.json_decode() json_decode — 对 JSON 格式的字符串进行编码 说明 mixed json_decode ( string $json [, bool ...
- ecshop类的解析2 json_encode和json_decode的具体实现
在看ecshop源码时,看到json这个类,研究了一下,它是为了兼容低版本php而做出的类,是对php的数据和json转换时使用的. encode和decode函数是对json的操作,对应json_e ...
- PHP开发笔记(二)PHP的json_encode和json_decode问题
解决PHP的json_encode问题之前,先了解一下PHP预定义常量http://php.net/manual/zh/json.constants.php. 用PHP的json_encode来处理中 ...
- php自定义json_encode()和json_decode()函数
json数据大家应该遇到过,json_encode()和json_decode()是php5.0以后加上的内置函数,如果低版本要使用,需加扩展,很多时候我们无权改变服务器的配置,我们只能通过自定义函数 ...
- 转载--PHP json_encode() 和json_decode()函数介绍
转自:http://www.nowamagic.net/php/php_FunctionJsonEncode.php 转自:http://www.jb51.net/article/30489.htm ...
- php 中利用json_encode和json_decode传递包括特殊字符的数据
</pre><span style="font-size:24px"></span><pre name="code" ...
随机推荐
- C# switch
要开学了(啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊) 做个沉默的行动者吧!(嘘嘘嘘嘘嘘嘘)今天去水题发现好多基础都不知道啊 1. switch(控制语句) { case 常量表达式:{statement ...
- 安装sqlserver2012时出现的丧心病狂的错误
Service Pack 安装程序 ------------------------------ 出现以下错误: 安装程序集“Microsoft.VC80.ATL,version="8.0. ...
- BZOJ 1461: 字符串的匹配
Description 同上题. Sol KMP+树状数组. 写这题的时候我灰常naive...不管了...直接贴代码... Code /******************************* ...
- Python PEP8规范
转载自:http://blog.csdn.net/kellyseeme/article/details/50644893 风格指南:http://zh-google-styleguide.readth ...
- PHP 文件下载流程
前台HTML: 添加download属性,不打开download.php页面 <a style='color:blue' href='download.php' download='data/C ...
- Java计算程序运行时间
public static void main(String[] args) { // TODO Auto-generated method stub long nd = 1000 * 24 * 60 ...
- C# 接口基础
接口只包含方法.属性.事件或索引器的签名. 实现接口的类或结构必须实现接口定义中指定的接口成员 接口中可以包含字段吗? 第一次被问到这个问题的时候被问愣住了,只能回答:印象当中没见过在接口中定义变量 ...
- 第七天 面向对象进阶与socket编程
1.静态方法(用得少)(解除某个函数跟类的关联,加了静态方法后,类便不能将类的参数传给静态方法函数了) class Dog(object): def __init__(self,name): @sta ...
- UTF8编码转换(C#)
例如: UTF8---ISO-8859-1 string string = "这是中文";Encoding utf8 = Encoding.UTF8; Encoding ISO = ...
- winrt组件库(包括翻书组件)
http://www.mindscapehq.com/products/metroelements/controls/book-control-for-winrt 点击“down free trial ...