PHP JSON and XML serialization/deserialization _php tutorial

Source: Internet
Author: User
Tags php json
In web development, the serialization and deserialization of objects are often used, more mainstream JSON format and XML format serialization and deserialization, today want to write a Jsop small demo, the results found not to use PHP serialization, check the data, make a note simple array JSON format serialization/ Deserialization

PHP provides Json_encode and Json_decode functions for JSON-formatted serialization/deserialization of objects

$data =array (' Name ' = ' Byron ', ' age ' =>24, ' Sex ' = ' Male ', ' Friends ' =>array (' Casper ', ' Frank ', ' Vincent ')); $json =json_encode ($data);//serializes an array into a JSON string echo $json. '
'; $array _json= Json_decode ($json);//Deserializes the JSON string into an 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 for XML-formatted serialization/deserialization of objects

$data =array (' Name ' = ' Byron ', ' age ' =>24, ' Sex ' = ' Male ', ' Friends ' =>array (' Casper ', ' Frank ', ' Vincent ')); $xml =wddx_serialize_value ($data);//serializes an array into an XML string echo $xml. '
'; $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
"; } else{echo "$key:"; foreach ($value as $current) {echo "$current";} Echo '
'; } }

Although the output format is strange due to the HTML transcoding, the serialized string is actually the same

In comparison with the JSON format, there are many more fields.
Complex object JSON format serialization/deserialization many times when we are working on an object that is not a simple array, but an array of objects we have customized, Json_encode and Json_decode are also capable. Customizing an object similar to the contents of the above array

Class Me {public $name; public $age; public $friends; function __construct ($name, $age, $friends) {$this->name= $name; $ This->age= $age; $this->friends= $friends; } }

$me 1=new Me (' Byron ', 24,array (' Casper ', ' Frank ', ' Vincent ')); $me 2=new Me (' Casper ', 25,array (' Byron ', ' Frank ', ' Vincent ')); $me 3=new Me (' Frank ', 26,array (' Casper ', ' Byron ', ' Vincent ')); Create a complex array of child elements that are custom classes that contain array fields in the custom class $array _me=array ($me 1, $me 2, $me 3); $json =json_encode ($array _me);//The serialized object array is the JSON string echo $json. '
'; $a =json_decode ($json);//Deserializes the JSON string into an array of objects foreach ($a as $AA) {echo $aa->name. '
'; }

You can see that the serialized string format is well-suited to expectations. Complex object XML format serialization/deserialization the same wddx_serialize_value and Wddx_deserialize functions can also perform XML format serialization/deserialization operations on complex objects, using just the object as an example

$me 1=new Me (' Byron ', 24,array (' Casper ', ' Frank ', ' Vincent ')); $me 2=new Me (' Casper ', 25,array (' Byron ', ' Frank ', ' Vincent ')); $me 3=new Me (' Frank ', 26,array (' Casper ', ' Byron ', ' Vincent ')); Create a complex array of child elements that are custom classes that contain array fields in the custom class $array _me=array ($me 1, $me 2, $me 3); $xml =wddx_serialize_value ($array _me);//Serializes an array of objects to an XML string echo $xml. '
'; $a =wddx_deserialize ($xml);//Deserializes an XML string into an array of objects foreach ($a as $AA) {echo $aa->name. '
'; }

The resulting XML string structure is like this


Finally beginner PHP, the article has many errors, I hope you criticize correct.

http://www.bkjia.com/PHPjc/824981.html www.bkjia.com true http://www.bkjia.com/PHPjc/824981.html techarticle in Web development, the serialization and deserialization of objects are often used, more mainstream JSON format and XML format serialization and deserialization, today want to write a Jsop small demo, the results found not ...

  • 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.