I have previously written a simple example of php returning json data. I just got online and suddenly found an article about json, which is quite detailed and worthy of reference. The content is as follows: from version 5.2, PHP native provides the json_encode () and json_decode () functions. The former is used for encoding and the latter is used for decoding. 1. Maid () 1234? Php $ arrar
I have previously written a simple example of php returning json data. I just got online and suddenly found an article about json, which is quite detailed and worthy of reference. The content is as follows: from version 5.2, PHP native provides the json_encode () and json_decode () functions. The former is used for encoding and the latter is used for decoding. I. json_encode () 1 2 3 4? Php $ arr = ar
I have previously written a simple example of php returning json data. I just got online and suddenly found an article about json, which is quite detailed and worthy of reference. The content is as follows:
Starting from version 5.2, PHP provides native json_encode () and json_decode () functions. The former is used for encoding and the latter is used for decoding.
I. json_encode ()
1 2 3 4 |
$arr = array ( 'a' =>1, 'b' =>2, 'c' =>3, 'd' =>4, 'e' =>5);
echo json_encode( $arr );
?>
|
Output
1 |
{ "a" :1, "b" :2, "c" :3, "d" :4, "e" :5}
|
Let's look at an example of object conversion:
1 2 3 4 5 6 |
$obj ->body = 'another post' ;
$obj ->id = 21;
$obj ->approved = true;
$obj ->favorite_count = 1;
$obj ->status = NULL;
echo json_encode( $obj );
|
Output
1 2 3 4 5 6 7 8 9 10 11 |
{
"body" : "another post" ,
"id" :21,
"approved" :true,
"favorite_count" :1,
"status" :null
}
|
Because json only accepts UTF-8 encoded characters, the json_encode () parameter must be UTF-8 encoded. Otherwise, null or null is obtained. Pay special attention to this when Chinese characters use GB2312 encoding or foreign languages use ISO-8859-1 encoding.
2. Index Array and associated array
PHP supports two types of Arrays: one is to save only the Index array (indexed array) of "value", and the other is to save "name/value) associated array (associative array ).
Because javascript does not support associating arrays, json_encode () only converts the Index array (indexed array) to the array format, and converts the associated array (associative array) to the object format.
For example, there is an index array
1 2 3 |
$arr = Array( 'one' , 'two' , 'three' );
echo json_encode( $arr );
|
Output
If you change it to an associated array:
1 2 3 |
$arr = Array( '1' => 'one' , '2' => 'two' , '3' => 'three' );
echo json_encode( $arr );
|
Output
1 |
{ "1" : "one" , "2" : "two" , "3" : "three" }
|
Note that the data format is changed from "[]" (array) to "{}" (object ).
If you want to forcibly convert an "index array" to "object", you can write
1 |
json_encode( (object) $arr );
|
Or
1 |
json_encode ( $arr , JSON_FORCE_OBJECT );
|
Iii. class Conversion
The following is a PHP class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Foo {
const ERROR_CODE = '404' ;
public $public_ex = 'this is public' ;
private $private_ex = 'this is private!' ;
protected $protected_ex = 'this should be protected' ;
public function getErrorCode() {
return self::ERROR_CODE;
}
}
|
Now, perform json conversion on the instance of this class:
1 2 3 4 5 |
$foo = new Foo;
$foo_json = json_encode( $foo );
echo $foo_json ;
|
The output result is
1 |
{ "public_ex" : "this is public" }
|
As you can see, except the public variable, other things (constants, private variables, methods, and so on) are lost.
Iv. json_decode ()
This function is used to convert json text to the corresponding PHP data structure. The following is an example:
1 2 3 4 5 |
$json = '{"foo": 12345}' ;
$obj = json_decode( $json );
print $obj ->{ 'foo' }; // 12345
|
In general, json_decode () always returns a PHP Object instead of an array. For example:
1 2 3 |
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ;
var_dump(json_decode( $json ));
|
The result is to generate a PHP Object:
1 2 3 4 5 6 7 8 9 10 |
object(stdClass)#1 (5) {
[ "a" ] => int(1)
[ "b" ] => int(2)
[ "c" ] => int(3)
[ "d" ] => int(4)
[ "e" ] => int(5)
}
|
If you want to forcibly generate a PHP join array, json_decode () requires a parameter true:
1 2 3 |
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ;
var_dump(json_decode( $json ,true));
|
The result generates an associated array:
1 2 3 4 5 6 7 8 9 10 |
array (5) {
[ "a" ] => int(1)
[ "b" ] => int(2)
[ "c" ] => int(3)
[ "d" ] => int(4)
[ "e" ] => int(5)
}
|
5. Common Errors of json_decode ()
The three json statements below are all incorrect. Can you tell where the error is?
1 2 3 4 5 |
$bad_json = "{ 'bar': 'baz' }" ;
$bad_json = '{ bar: "baz" }' ;
$bad_json = '{ "bar": "baz", }' ;
|
If json_decode () is executed on these three strings, null is returned and an error is returned.
The first error is that the json separator (delimiter) only allows double quotation marks and does not support single quotation marks. The second error is the "name" of the json name-Value Pair (the part on the left of the colon). Double quotation marks must be used in any case. The third error is that you cannot add a comma (trailing comma) after the last value ).
In addition, json can only be used to represent objects and arrays. If json_decode () is used for a string or value, null is returned.
1 |
var_dump(json_decode( "Hello World" )); //null
|