Php json and xml serialization/deserialization

Source: Internet
Author: User
Tags php json

In web development, Object serialization and deserialization are often used. The mainstream formats include json format and xml format serialization and deserialization. Today I want to write a small jsop demo, the result shows that php serialization is not used. I checked the information and made a note to serialize/deserialize the json format of a simple array.

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

$ Data = array ('name' => 'byron ', 'age' => 24, 'sex' => 'male ', 'friends' => array ('casper ', 'frank', 'Vincent'); $ json = json_encode ($ data ); // serialize the array into a json string echo $ json. '<br/>'; $ array_json = json_decode ($ json); // deserializes A json string into an array while (list ($ key, $ value) = each ($ array_json) {if (! Is_array ($ value) {echo "$ key: $ value <br/>" ;}else {echo "$ key:"; foreach ($ value as $ current) {echo "$ current success";} echo '<br/> ';}}

Simple array xml format serialization/deserialization

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

$ Data = array ('name' => 'byron ', 'age' => 24, 'sex' => 'male ', 'friends' => array ('casper ', 'frank', 'Vincent'); $ xml = wddx_serialize_value ($ data ); // serialize the array into an xml string echo $ xml. '<br/>'; $ array_xml = wddx_deserialize ($ xml); // deserializes an xml string into an array while (list ($ key, $ value) = each ($ array_xml) {if (! Is_array ($ value) {echo "$ key: $ value <br/>" ;}else {echo "$ key:"; foreach ($ value as $ current) {echo "$ current success";} echo '<br/> ';}}

 

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

Compared with the json format, many fields are added.
Serialization/deserialization of complex objects in json format many times when we operate, the processed objects are not a simple array, but an array of custom objects, json_encode and json_decode are also competent. Customizes an object similar to the preceding array content

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. The child element is a custom class. The custom class contains the array field $ array_me = array ($ me1, $ me2, $ me3); $ json = json_encode ($ array_me); // The serialized object array is a json string echo $ json. '<br/>'; $ a = json_decode ($ json); // deserializes a json string into an object array foreach ($ a as $ aa) {echo $ aa-> name. '<br/> ';}

 

 

We can see that the serialized string format is very consistent with expectations. The same wddx_serialize_value and wddx_deserialize functions can be used to serialize or deserialize complex objects in xml format. The preceding example is used.

$ 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. The child element is a custom class. The custom class contains the array field $ array_me = array ($ me1, $ me2, $ me3); $ xml = wddx_serialize_value ($ array_me); // The serialized object array is an xml string echo $ xml. '<br/>'; $ a = wddx_deserialize ($ xml); // deserializes an xml string into an object array foreach ($ a as $ aa) {echo $ aa-> name. '<br/> ';}

The generated xml string structure is as follows:


At last, php is a beginner. I hope you will criticize and correct this article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.