What is json?
JSON (JavaScript Object Notation) is a lightweight data exchange format mainly used to transmit data.
JSON can convert a set of data represented in a JavaScript object into a string, which can then be easily passed between functions, or from a web client to a server in an asynchronous application program. This string looks a little weird, but JavaScript can easily interpret it, and JSON can represent more complex structures than "name/value pairs". For example, arrays and complex objects can be represented rather than just simple lists of keys and values.
This chapter is for understanding, no need to focus on mastering.
Environment configuration
JSON extension has been built-in in php5.2.0 and above.
JSON function
Syntax
string json_encode ( $value [, $options = 0 ] )Parameters
· value: The value to be encoded. This function is only valid for UTF-8 encoded data. · options: Binary mask consisting of the following constants: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECTExample
The following example demonstrates how to convert a PHP array into JSON format data:<?php $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); echo json_encode($arr); ?>The execution result of the above code is: {"a":1,"b":2,"c ":3,"d":4,"e":5}The following example demonstrates how to convert PHP objects into JSON format data:
<?php class Emp { public $name = ""; public $hobbies = ""; public $birthdate = ""; } $e = new Emp(); $e->name = "sachin"; $e->hobbies = "sports"; $e->birthdate = date('m/d/Y h:i:s a', "8/5/1974 12:20:03 p"); $e->birthdate = date('m/d/Y h:i:s a', strtotime("8/5/1974 12:20:03")); echo json_encode($e); ?>The execution result of the above code is:{"name":"sachin","hobbies":"sports","birthdate":"08\/05\/1974 12:20:03 pm"}
Syntax
mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])Parameters
· json_string: The JSON string to be decoded, must be UTF-8 encoded data· assoc: When this parameter is TRUE, it will be returned Array, returns object if FALSE. · depth: A parameter of integer type, which specifies the recursion depth · options: Binary mask, currently only JSON_BIGINT_AS_STRING is supported.Example
The following example demonstrates how to decode JSON data:<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); var_dump(json_decode($json, true)); ?>
The execution result of the above code is:
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)
}
Json format rules in PHP
- Parallel data are separated by commas (", ")
- Mapping is done by colon ( ": ") represents
- The collection (array) of parallel data is represented by square brackets ("[]")
- The mapped collection (object) is represented by curly brackets ("{}")
The following sentence:
"Beijing has an area of 16,800 square kilometers and a permanent population of 16 million. Shanghai has an area of 6,400 square kilometers and a permanent population of 18 million."
It is written in json format like this:
[
{"City":"Beijing","Area":16800,"Population":1600},
{"City" ":"Shanghai","Area":6400,"Population":1800}
]
Function to operate json in PHP
Encryption json_encode
Decrypt json_decode
Conversion of one-dimensional array to json data format
<?php $arr_1 = array(); $arr_1['username'] = 'lisi'; $arr_1['age'] = 20; echo json_encode($arr_1);//{"username":"lisi","age":20} ?>
Conversion of multi-dimensional array to json data format
<?php $arr_2 = array(); // 三维数组 $arr_2['member']['lisi']['job'] = "worker"; $arr_2['member']['lisi']['age'] = 30; $arr_2['member']['wangwu']['job'] = "student"; $arr_2['member']['wangwu']['age'] = 10; echo json_encode($arr_2); //{"member":{"lisi":{"job":"worker","age":30},"wangwu":{"job":"student","age":10}}} ?>
Conversion of object to json data format
When the object is converted to json data, only public variables are converted, private variables are not converted
<?php class Person{ public $name = "public name"; protected $ptName = "protected name"; private $pName = "private name"; public function sayName(){ return $this->name; } } $person1 = new Person(); echo json_encode($person1);//{"name":"public name"} ?>
Convert json data format to object type
<?php $jsonStr = '{"key1":"value1","key2":"value2"}'; print_r(json_decode($jsonStr,false));//stdClass Object ( [key1] => value1 [key2] => value2 ?>
Convert json data format to array type
<?php $jsonStr = '{"key1":"value1","key2":"value2"}'; print_r(json_decode($jsonStr,true));//Array ( [key1] => value1 [key2] => value2 ) ?>
json_decode($jsonStr ,true); If the second parameter is true, the result will be converted to array type. The parameter defaults to false and is converted to an object by default