Home > Backend Development > PHP Tutorial > Solution to the problem that php json_encode does not support object private attributes

Solution to the problem that php json_encode does not support object private attributes

jacklove
Release: 2023-04-01 11:58:01
Original
3368 people have browsed it

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

<?php// 用户类class user{
    public $id = 1;    public $name = &#39;fdipzone&#39;;    public $profession = &#39;programmer&#39;;    private $age = 18;
}// 对象$oUser = new User;// json_encode$json = json_encode($oUser);echo $json;// json_decode$oUser = json_decode($json);
var_dump($oUser);?>
Copy after login
Copy after login

Output:

{"id":1,"name":"fdipzone","profession":"programmer"}object(stdClass)[2]  public &#39;id&#39; => int 1
  public &#39;name&#39; => string &#39;fdipzone&#39; (length=8)  public &#39;profession&#39; => string &#39;programmer&#39; (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:

<?php// 用户类class user implements JsonSerializable{
    public $id = 1;    public $name = &#39;fdipzone&#39;;    public $profession = &#39;programmer&#39;;    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);?>
Copy after login
Copy after login

Output:

{"id":1,"name":"fdipzone","profession":"programmer","age":18}object(stdClass)[2]  public &#39;id&#39; => int 1
  public &#39;name&#39; => string &#39;fdipzone&#39; (length=8)  public &#39;profession&#39; => string &#39;programmer&#39; (length=10)  public &#39;age&#39; => 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

<?php// 用户类class user{
    public $id = 1;    public $name = &#39;fdipzone&#39;;    public $profession = &#39;programmer&#39;;    private $age = 18;
}// 对象$oUser = new User;// json_encode$json = json_encode($oUser);echo $json;// json_decode$oUser = json_decode($json);
var_dump($oUser);?>
Copy after login
Copy after login

Output:

{"id":1,"name":"fdipzone","profession":"programmer"}object(stdClass)[2]  public &#39;id&#39; => int 1
  public &#39;name&#39; => string &#39;fdipzone&#39; (length=8)  public &#39;profession&#39; => string &#39;programmer&#39; (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:

<?php// 用户类class user implements JsonSerializable{
    public $id = 1;    public $name = &#39;fdipzone&#39;;    public $profession = &#39;programmer&#39;;    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);?>
Copy after login
Copy after login

Output:

{"id":1,"name":"fdipzone","profession":"programmer","age":18}object(stdClass)[2]  public &#39;id&#39; => int 1
  public &#39;name&#39; => string &#39;fdipzone&#39; (length=8)  public &#39;profession&#39; => string &#39;programmer&#39; (length=10)  public &#39;age&#39; => 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template