您的位置:首页 > 编程语言 > PHP开发

PHP和JSON

2016-01-29 23:13 706 查看

JSON基本语法

并列的数据之间用(英文半角)逗号,分隔

映射用(英文半角)冒号:表示

并列的数据的集合(数组)用(英文半角)方括号[]表示

映射的集合(对象)用(英文半角)大括号{}表示

如果只有一条记录,json格式如下:

{"id":101,"name":"郭靖","job":"保安队长"}


如果有多条记录,json格式如下:

[
{"id":101,"name":"郭靖","job":"保安队长"},
{"id":102,"name":"黄蓉","job":"秘书"},
{"id":110,"name":"黄老邪","job":"CTO"}
]


PHP操作JSON

php操作json主要使用json_encode()和json_decode()这两个函数。

json_encode()

json_encode()可以将其他数据类型格式的数据(主要是数组)转化为json数据格式:

<?php
header("Content-Type:text/html;charset=utf-8");

$arr = array (
"id"=>101,
"name"=>"郭靖",
"job"=>"保安队长");
$json1 = json_encode($arr);
echo $json1;
echo "<hr/>";
$arr = array (
array(
"id"=>101,
"name"=>"郭靖",
"job"=>"保安队长"
),
array(
"id"=>102,
"name"=>"Naruto",
"job"=>"CEO"
),
array(
"id"=>110,
"name"=>"黄老邪",
"job"=>"CTO"
)
);
$json2 = json_encode($arr);
echo $json2;


输出结果:

{"id":101,"name":"\u90ed\u9756","job":"\u4fdd\u5b89\u961f\u957f"}

[
{"id":101,"name":"\u90ed\u9756","job":"\u4fdd\u5b89\u961f\u957f"},
{"id":102,"name":"Naruto","job":"CEO"},{"id":110,"name":"\u9ec4\u8001\u90aa","job":"CTO"}
]


json_encode会将其中的汉字转化为ASCII码

json_decode()

json_decode可以将json转换为对象或者数组,其第二个参数若为true则可以将json转换为数组,否则转换为对象。

若json只有一个集合(一条记录),无并列数据,则可转换为对象或一维数组;若json为包含多个并列集合(多条数据),则可转换为对象数组或二维数组。

<?php
header("Content-Type:text/html;charset=utf-8");

$json1 = '{"id":101,"name":"\u90ed\u9756","job":"\u4fdd\u5b89\u961f\u957f"}';
echo $json1."<br/>";
$obj1 = json_decode($json1);
$arr1 = json_decode($json1,true);
var_dump($obj1);
var_dump($arr1);
echo "<hr/>";
$json2 = '[{"id":101,"name":"\u90ed\u9756","job":"\u4fdd\u5b89\u961f\u957f"},{"id":102,"name":"Naruto","job":"CEO"},{"id":110,"name":"\u9ec4\u8001\u90aa","job":"CTO"}]';
echo $json2."<br/>";
$objarr = json_decode($json2);
$arr2 = json_decode($json2,true);
var_dump($objarr);
var_dump($arr2);


输出结果:

{"id":101,"name":"\u90ed\u9756","job":"\u4fdd\u5b89\u961f\u957f"}
object(stdClass)[1]
public 'id' => int 101
public 'name' => string '郭靖' (length=6)
public 'job' => string '保安队长' (length=12)
array (size=3)
'id' => int 101
'name' => string '郭靖' (length=6)
'job' => string '保安队长' (length=12)
[{"id":101,"name":"\u90ed\u9756","job":"\u4fdd\u5b89\u961f\u957f"},{"id":102,"name":"Naruto","job":"CEO"},{"id":110,"name":"\u9ec4\u8001\u90aa","job":"CTO"}]
array (size=3)
0 =>
object(stdClass)[2]
public 'id' => int 101
public 'name' => string '郭靖' (length=6)
public 'job' => string '保安队长' (length=12)
1 =>
object(stdClass)[3]
public 'id' => int 102
public 'name' => string 'Naruto' (length=6)
public 'job' => string 'CEO' (length=3)
2 =>
object(stdClass)[4]
public 'id' => int 110
public 'name' => string '黄老邪' (length=9)
public 'job' => string 'CTO' (length=3)
array (size=3)
0 =>
array (size=3)
'id' => int 101
'name' => string '郭靖' (length=6)
'job' => string '保安队长' (length=12)
1 =>
array (size=3)
'id' => int 102
'name' => string 'Naruto' (length=6)
'job' => string 'CEO' (length=3)
2 =>
array (size=3)
'id' => int 110
'name' => string '黄老邪' (length=9)
'job' => string 'CTO' (length=3)


json_decode会将其中的ASCII码转换为汉字

Serialize函数

php中还有一对函数是serialize()和unserialize()函数,可以将数据(常为数组)序列化为字符串,以便于存储和交互,也能很方便的由字符串还原数据。

serialize()函数与json_encode()函数有些类似,但是区别也很大,serialize()函数转化成的字符串更为复杂且详细,json_ecode()转化为的json数据可以和其他语言更好地交互。

serialize()函数

<?php
header("Content-Type:text/html;charset=utf-8");

$arr = array (
"id"=>101,
"name"=>"郭靖",
"job"=>"保安队长");
$s1 = serialize($arr);
echo $s1;
echo "<hr/>";
$arr = array (
array(
"id"=>101,
"name"=>"郭靖",
"job"=>"保安队长"
),
array(
"id"=>102,
"name"=>"Naruto",
"job"=>"CEO"
),
array(
"id"=>110,
"name"=>"黄老邪",
"job"=>"CTO"
)
);
$s2 = serialize($arr);
echo $s2;


输出结果:

a:3:{s:2:"id";i:101;s:4:"name";s:6:"郭靖";s:3:"job";s:12:"保安队长";}
a:3:{i:0;a:3:{s:2:"id";i:101;s:4:"name";s:6:"郭靖";s:3:"job";s:12:"保安队长";}i:1;a:3:{s:2:"id";i:102;s:4:"name";s:6:"Naruto";s:3:"job";s:3:"CEO";}i:2;a:3:{s:2:"id";i:110;s:4:"name";s:9:"黄老邪";s:3:"job";s:3:"CTO";}}


unserialize()函数

<?php
header("Content-Type:text/html;charset=utf-8");

$str1 = 'a:3:{s:2:"id";i:101;s:4:"name";s:6:"郭靖";s:3:"job";s:12:"保安队长";}';
echo $str1."<br/>";
$arr1 = unserialize($str1);
var_dump($arr1);
echo "<hr/>";
$str2 = 'a:3:{i:0;a:3:{s:2:"id";i:101;s:4:"name";s:6:"郭靖";s:3:"job";s:12:"保安队长";}i:1;a:3:{s:2:"id";i:102;s:4:"name";s:6:"Naruto";s:3:"job";s:3:"CEO";}i:2;a:3:{s:2:"id";i:110;s:4:"name";s:9:"黄老邪";s:3:"job";s:3:"CTO";}}';
echo $str2."<br/>";
$arr2 = unserialize($str2);
var_dump($arr2);


输出结果:

a:3:{s:2:"id";i:101;s:4:"name";s:6:"郭靖";s:3:"job";s:12:"保安队长";}
array (size=3)
'id' => int 101
'name' => string '郭靖' (length=6)
'job' => string '保安队长' (length=12)
a:3:{i:0;a:3:{s:2:"id";i:101;s:4:"name";s:6:"郭靖";s:3:"job";s:12:"保安队长";}i:1;a:3:{s:2:"id";i:102;s:4:"name";s:6:"Naruto";s:3:"job";s:3:"CEO";}i:2;a:3:{s:2:"id";i:110;s:4:"name";s:9:"黄老邪";s:3:"job";s:3:"CTO";}}
array (size=3)
0 =>
array (size=3)
'id' => int 101
'name' => string '郭靖' (length=6)
'job' => string '保安队长' (length=12)
1 =>
array (size=3)
'id' => int 102
'name' => string 'Naruto' (length=6)
'job' => string 'CEO' (length=3)
2 =>
array (size=3)
'id' => int 110
'name' => string '黄老邪' (length=9)
'job' => string 'CTO' (length=3)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: