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" ...
随机推荐
- Codeforces 260 C. Boredom
题目链接:http://codeforces.com/contest/456/problem/C 解题报告:给出一个序列,然后选择其中的一个数 k 删除,删除的同时要把k - 1和k + 1也删除掉, ...
- shareSDK
一. 编写sharesdk代码 在AppDelegate.m中 //shareSDK相关 #import <ShareSDK/ShareSDK.h> #import "Weibo ...
- Palindrome Partitioning II Leetcode
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Swift初学有一点难理解的东西,整理了一下,想明白了。
func makeIncrementer() -> (Int -> Int) { func addOne(number: Int) -> Int { ...
- net-snmp配置文件详解
net-snmp配置文件详解 net-snmp的配置文件是有一定的层次结构的,配置起来也很方便.网上找了很多资料,大概把这个配置文件的各个信息搞懂了一点.其实在net-snmp的EXAMPLE.con ...
- C语言的执行
在ANSIC 的任何一种实现中,有两种不同的环境 翻译环境:将源代码转换为可执行的机器代码 执行环境:用于执行代码 这两种环境可以运行于同一个机器上,也可以运行于不同的机器上 例如交叉编译器:在一台机 ...
- linux系统下获取IP,MAC,子网掩码,网关
获取IP和子网掩码 int getLocalInfo(char IP[],char Mask[]) { int fd; int interfaceNum = 0; struct ifreq buf[1 ...
- gjd
#include <cstdio> #include <cstring> #include <malloc.h> #define radix (1u<< ...
- 【GoLang】GoLang struct 使用
代码示例: package main import "fmt" type Human struct { name string age int weight int } type ...
- NPOI教程
NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. NPOI 官方网站:http://npoi.codeplex.com/( ...