键值对的集合,不同的语言中可表示为对象(object),记录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),关联数组(associative array)等,大部分语言中,表示数组(array)
JSON(Javascript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成
参考PHP Manual
JSON形式:{ "name1" : value1 , "name2" : value2}
array形式:[ "name1" => value1 , 'name2" => value2] / array("name1" => value1 , 'name2" => value2) ,array获取具体的值array['name1']
object对象 :(object获取具体值的方法 $obj->name1)
$obj1 = new \stdClass; // Instantiate stdClass object
$obj2 = new class{}; // Instantiate anonymous class
$obj3 = (object)[]; // Cast empty array to object
JSON函数
1、json_decode - 对JSON格式的字符串进行解码,将一个JSON编码的字符串并且把它转换为PHP变量
json_decode ( string $json
[, bool $assoc
= FALSE
[, int $depth
= 512 [, int $options
= 0 ]]] ) : mixed
$json 表示带解码的json string 格式的字符串
assoc 该参数为true时,将返回array,而非object(不带该参数默认返回object)
depth 指定递归深度
返回值:通过恰当的 PHP 类型返回在 json
中编码的数据。值true, false 和 null 会相应地返回 TRUE
, FALSE
和 NULL
。 如果 json
无法被解码, 或者编码数据深度超过了递归限制的话,将会返回NULL
。
eg:$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
//object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) }
var_dump(json_decode($json, true)); //
array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) }
2、json_encode - 对变量进行JSON编码
json_encode ( mixed $value
[, int $options
= 0 [, int $depth
= 512 ]] ) : string
返回值:成功则返回 JSON 编码的 string 或者在失败时返回 FALSE
。
eg:$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr); //
{"a":1,"b":2,"c":3,"d":4,"e":5}