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" ...
随机推荐
- leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- C,C++经典笔试题(答案)转自:http://blog.163.com/jianhuali0118@126/blog/static/377499702008230104125229/
一.请填写BOOL , float, 指针变量 与“零值”比较的 if 语句.(10分) 请写出 BOOL flag 与“零值”比较的 if 语句.(3分) 标准答案: if ( fla ...
- bufferedimage 转换成 inputstream并保存文件
BufferedImage img = removeBackgroud(file);//去除重影 //bufferedimage 转换成 inputstream ByteArrayOutputStre ...
- HttpGet
private static void func_httpGet(String url) { HttpClient httpClient = new HttpClient(); try { GetMe ...
- Extjs TabPanel 选项卡延迟加载
Extjs TabPanel 选项卡延迟加载 说明: Ext中用到tabpanel选项卡控件, 选项卡页签默认是延迟加载的, 当用户手工切换到某页签下时该页签才会加载, 在页签没有加载前, 用户对该页 ...
- Xcode 6 正式版如何创建一个Empty Application
Xcode 6 正式版里面没有Empty Application这个模板,这对于习惯了纯代码编写UI界面的程序员来说很不习惯. 有网友给出了一个解决方法是,把Xcode 6 beta版里面的模板复制过 ...
- php内核和瓦力上线部署
http://www.php-internals.com/ http://www.walle-web.io/
- 【Other】Ubuntu 14.04 pptp 客户端连接配置
sudo apt-get install pptp-linux binutils modprobe ppp_mppe sudo pptpsetup --create testvpn --server ...
- git stash简介
原文:http://gitbook.liuhui998.com/4_5.html 一.基本操作 当你正在做一项复杂的工作时, 发现了一个和当前工作不相关但是又很讨厌的bug. 你这时想先修复bug再做 ...
- struts2 复杂参数封装
1.1.1 Struts2中封装复杂类型的数据: 封装到List集合: 页面: 商品名称:<input type="text" name="products[ ...