This article introduces the solution for php json_encode that does not support the private properties of objects. json_encode can convert the object into json format, and use json_decode to restore it to the object.
But if the object contains private attributes, the private attributes will be lost after executing json_encode.
Example: json_encode loses object private attributes
1 2 3 4 | <?php
public $id = 1; public $name = 'fdipzone'; public $profession = 'programmer'; private $age = 18;
}
var_dump( $oUser );?>
|
Copy after login
Copy after login
Output:
1 2 | { "id" :1, "name" : "fdipzone" , "profession" : "programmer" }object(stdClass)[2] public 'id' => int 1
public 'name' => string 'fdipzone' (length=8) public 'profession' => string 'programmer' (length=10)
|
Copy after login
Copy after login
After executing json_encode, the private attribute age is lost.
Solution to the loss of private attributes of the object after json_encode
We can modify the class to implement JsonSerializableInterface method of abstract classjsonSerialize, specify Data that needs to be serialized into JSON.
For the JsonSerializable::jsonSerialize method, please refer to the official website: http://php.net/manual/zh/jsonserializable.jsonserialize.php
After modification The code is as follows:
1 2 3 4 5 6 7 8 | <?php
public $id = 1; public $name = 'fdipzone'; public $profession = 'programmer'; private $age = 18;
public function jsonSerialize() {
$data = []; foreach ( $this as $key => $val ){ if ( $val !== null) $data [ $key ] = $val ;
} return $data ;
}
}
var_dump( $oUser );?>
|
Copy after login
Copy after login
Output:
1 2 | { "id" :1, "name" : "fdipzone" , "profession" : "programmer" , "age" :18}object(stdClass)[2] public 'id' => int 1
public 'name' => string 'fdipzone' (length=8) public 'profession' => string 'programmer' (length=10) public 'age' => int 18
|
Copy after login
Copy after login
After specifying the data that needs to be serialized into JSON, json_encode can read the private attribute age.
This article introduces the solution to the problem that php json_encode does not support the private properties of objects. json_encode can convert the object into json format, and use json_decode to restore it to the object.
But if the object contains private attributes, the private attributes will be lost after executing json_encode.
Example: json_encode loses object private attributes
1 2 3 4 | <?php
public $id = 1; public $name = 'fdipzone'; public $profession = 'programmer'; private $age = 18;
}
var_dump( $oUser );?>
|
Copy after login
Copy after login
Output:
1 2 | { "id" :1, "name" : "fdipzone" , "profession" : "programmer" }object(stdClass)[2] public 'id' => int 1
public 'name' => string 'fdipzone' (length=8) public 'profession' => string 'programmer' (length=10)
|
Copy after login
Copy after login
After executing json_encode, the private attribute age is lost.
Solution to the loss of private attributes of the object after json_encode
We can modify the class to implement JsonSerializableInterface method of abstract classjsonSerialize, specify Data that needs to be serialized into JSON.
For the JsonSerializable::jsonSerialize method, please refer to the official website: http://php.net/manual/zh/jsonserializable.jsonserialize.php
After modification The code is as follows:
1 2 3 4 5 6 7 8 | <?php
public $id = 1; public $name = 'fdipzone'; public $profession = 'programmer'; private $age = 18;
public function jsonSerialize() {
$data = []; foreach ( $this as $key => $val ){ if ( $val !== null) $data [ $key ] = $val ;
} return $data ;
}
}
var_dump( $oUser );?>
|
Copy after login
Copy after login
Output:
1 2 | { "id" :1, "name" : "fdipzone" , "profession" : "programmer" , "age" :18}object(stdClass)[2] public 'id' => int 1
public 'name' => string 'fdipzone' (length=8) public 'profession' => string 'programmer' (length=10) public 'age' => int 18
|
Copy after login
Copy after login
After specifying the data that needs to be serialized into JSON, json_encode can read the private attribute age.
This article explains the solution to the problem that php json_encode does not support object private attributes. For more related content, please pay attention to the php Chinese website.
Related recommendations:
js basic data types and conversion operators
Usage of with in JavaScript
js and php nesting
The above is the detailed content of Solution to the problem that php json_encode does not support object private attributes. For more information, please follow other related articles on the PHP Chinese website!