1.json_decode ()
Json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_decode-encoding a JSON-formatted string
Description
Mixed Json_decode (String $json [, bool $assoc])
Accept a JSON-formatted string and convert it to a PHP variable
Parameters
Json
String in JSON string format to decode.
Assoc
When this argument is TRUE, an array is returned instead of an object.
return value
Returns an object or if the optional assoc parameter are TRUE, an associative array is instead returned.
Example of Json_decode ()
<? $json = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} 'var_dump(Json_decode ($json var_dump(Json_decode ($jsontrue
Results
Object (StdClass) # ["a"] = Int (1) ["B"] = + int (2) ["C"] = + int (3) ["D"] + int (4) [ "E"] = + int (5Array(5) {["a"] = + int (1) ["b"] = = Int (2) ["C"] = > int (3) ["D"] = + int (4) ["E"] + int (5
You can see an associative array of Json_decode ($data, true) output, so Json_decode ($data) outputs an object, and Json_decode ("$arr", true) is forcing it to generate a PHP associative array.
2.json_encode ()
Json_encode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_encode-JSON Encoding of variables
Report a bug description
String Json_encode (mixed $value [, int $options = 0])
Returns the JSON form of value values
Report a bug parameter
Value
The value to encode, in addition to the resource type, can be any data type
This function can only accept UTF-8 encoded data.
Report a bug return value
A successful encoding returns a string in JSON form or FALSE on failure.
Json_encode () Example
<? $arrarray (' A ' =>1, ' B ' =>2, ' C ' =>3, ' d ' =>4, ' e ' =>5Echo Json_encode ($arr?>
Results
As you can see, Json_encode () and Json_decode () are compiled and deserialized, and note that JSON only accepts UTF-8 encoded characters, so the parameters of Json_encode () must be utf-8 encoded, otherwise they will be null or empty.
The transcoding format uses the following function
<? PHP $arr=iconv(' GBK ', ' utf-8 ',$arr);
JSON array to normal array normal array to JSON array