Home > Article > Backend Development > Solution to the problem that php json_encode does not support object private attributes
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.
<?php// 用户类class user{ public $id = 1; public $name = 'fdipzone'; public $profession = 'programmer'; private $age = 18; }// 对象$oUser = new User;// json_encode$json = json_encode($oUser);echo $json;// json_decode$oUser = json_decode($json); var_dump($oUser);?>
Output:
{"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)
After executing json_encode, the private attribute age is lost.
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:
<?php// 用户类class user implements JsonSerializable{ public $id = 1; public $name = 'fdipzone'; public $profession = 'programmer'; private $age = 18; // 实现的抽象类方法,指定需要被序列化JSON的数据 public function jsonSerialize() { $data = []; foreach ($this as $key=>$val){ if ($val !== null) $data[$key] = $val; } return $data; } }// 对象$oUser = new User;// json_encode$json = json_encode($oUser);echo $json;// json_decode$oUser = json_decode($json); var_dump($oUser);?>
Output:
{"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
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.
<?php// 用户类class user{ public $id = 1; public $name = 'fdipzone'; public $profession = 'programmer'; private $age = 18; }// 对象$oUser = new User;// json_encode$json = json_encode($oUser);echo $json;// json_decode$oUser = json_decode($json); var_dump($oUser);?>
Output:
{"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)
After executing json_encode, the private attribute age is lost.
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:
<?php// 用户类class user implements JsonSerializable{ public $id = 1; public $name = 'fdipzone'; public $profession = 'programmer'; private $age = 18; // 实现的抽象类方法,指定需要被序列化JSON的数据 public function jsonSerialize() { $data = []; foreach ($this as $key=>$val){ if ($val !== null) $data[$key] = $val; } return $data; } }// 对象$oUser = new User;// json_encode$json = json_encode($oUser);echo $json;// json_decode$oUser = json_decode($json); var_dump($oUser);?>
Output:
{"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
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
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!