Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand php json and xml serialization / deserialization

Shulou Source: shulou.com Published: 2022-06-03 07:39:51 10月04日 Update

This article introduces "how to understand php json and xml serialization / deserialization" related knowledge, in the actual case operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Serialization and deserialization of objects are often used in web development. The mainstream ones are serialization and deserialization of json format and xml format. Today, I want to write a small demo of jsop, but I found that I can't use php serialization. I checked the data and took notes on simple array json format serialization / deserialization.

Php provides json_encode and json_decode functions to serialize / deserialize objects in json format

$data=array ('Name'= >' Byron','Age'= > 24 Male','Friends'= > array ('Casper','Frank','Vincent')); $json=json_encode ($data); / / serialize the array into the json string echo $json.''; $array_json= json_decode ($json); / / deserialize the json string into the array while (list ($key,$value) = each ($array_json)) {if (! is_array ($value)) {echo "$key: $value" } else {echo "$key:"; foreach ($value as $current) {echo "$current  ";} echo';}}

Simple array xml format serialization / deserialization

Php provides wddx_serialize_value and wddx_deserialize functions to serialize / deserialize objects in xml format

$data=array ('Name'= >' Byron','Age'= > 24 Male','Friends'= > array ('Casper','Frank','Vincent')); $xml=wddx_serialize_value ($data); / / serialize the array into the xml string echo $xml.''; $array_xml=wddx_deserialize ($xml); / / deserialize the xml string into the array while (list ($key,$value) = each ($array_xml)) {if (! is_array ($value)) {echo "$key: $value" } else {echo "$key:"; foreach ($value as $current) {echo "$current  ";} echo';}}

Although the output format is strange due to HTML transcoding, the serialized string looks like this

Compared with json format, there are many more fields.

Complex object json format serialization / deserialization many times when we operate, the object we deal with is not a simple array, but an array of objects that we customize, and json_encode and json_decode are also competent. Customize an object similar to the contents of the array above

Class Me {public $name; public $age; public $friends; function _ construct ($name,$age,$friends) {$this- > name=$name; $this- > age=$age; $this- > friends=$friends;}} $me1=new Me ('Byron',24,array (' Casper','Frank','Vincent')); $me2=new Me ('Casper',25,array (' Byron','Frank','Vincent')); $me3=new Me ('Frank',26,array (' Casper','Byron','Vincent')) / / create a complex array with child elements of a custom class that contains array fields $array_me=array ($me1,$me2,$me3); $json=json_encode ($array_me); / / serializes the object array as the json string echo $json.''; $a=json_decode ($json); / / deserializes the json string into the object array foreach ($an as $aa) {echo $aa- > name.'';}

You can see that the serialized string format works well as expected. Serialization / deserialization of complex objects in xml format similarly wddx_serialize_value and wddx_deserialize functions are also competent for serialization / deserialization of complex objects in xml format, using the object just mentioned as an example

$me1=new Me ('Byron',24,array (' Casper','Frank','Vincent')); $me2=new Me ('Casper',25,array (' Byron','Frank','Vincent')); $me3=new Me ('Frank',26,array (' Casper','Byron','Vincent')); / / create a complex array with subelements of a custom class that contains array fields $array_me=array ($me1,$me2,$me3); $xml=wddx_serialize_value ($array_me) / serialize the object array as the xml string echo $xml.''; $a=wddx_deserialize ($xml); / / deserialize the xml string into the object array foreach ($an as $aa) {echo $aa- > name.'';}

The generated xml string structure looks like this

This is the end of "how to understand php json and xml serialization / deserialization". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Sequence array object format character string complexity content function field element time more knowledge package output utility achievement next mainstream Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Microsoft Shulou Technology Xiaomi