The solution to the problem that php does not support json_decode: first implement the interface method jsonSerialize of the JsonSerializable abstract class; then specify the data that needs to be serialized into JSON.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
What should I do if php does not support json_decode? php json_encode does not support object private attributes. This article introduces the solution to php json_encode not supporting object private attributes. json_encode can convert objects into json format, and json_decode can be used to restore objects.
But if the object contains private attributes, the private attributes will be lost after json_encode is executed.
Example: json_encode loses object private attributes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Output:
1 2 3 4 5 |
|
After executing json_encode, the private attribute age is lost.
Solution to the loss of private attributes of the object after json_encodeWe can modify the class so that it implements the interface method jsonSerialize of the JsonSerializable abstract class, specifying what needs to be serialized into JSON data.
For the JsonSerializable::jsonSerialize method, please refer to the official website: http://php.net/manual/zh/jsonserializable.jsonserialize.php
The modified code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Output:
1 2 3 4 5 6 |
|
After specifying the data that needs to be serialized into JSON, json_encode can read the private attribute age.
Recommended learning: "
PHP Video TutorialThe above is the detailed content of What should I do if php does not support json_decode?. For more information, please follow other related articles on the PHP Chinese website!