Home >Backend Development >PHP Tutorial >php json vs. xml serialization/deserialization
The serialization and deserialization of objects are often used in web development. The more mainstream ones are the serialization and deserialization of json format and xml format. Today I wanted to write a small demo of jsop, but I found that I can't use php serialization. I checked the information and made a note. 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,'Sex'=>'Male','Friends'=>array('Cas

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,'Sex'=>'Male','Friends'=>array('Cas


Complex object json format serialization/deserialization. Many times when we are operating, the object we process is not a simple array, but a custom object. Arrays, json_encode and json_decode are also competent. Customize an object similar to the above 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'));
//创建一个复杂的数组,子元素是自定义类,自定义类中包含数组字段
$array_me=array($me1,$me2,$me3); $json=json_encode($array_me);
//序列化对象数组为json字符串 echo $json.'<br/>'; $a=json_decode($json);
//将json字符串反序列化为对象数组 foreach ($a as $aa) { echo $aa->name.'<br/>'; }

$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'));
//创建一个复杂的数组,子元素是自定义类,自定义类中包含数组字段
$array_me=array($me1,$me2,$me3); $xml=wddx_serialize_value($array_me);
//序列化对象数组为xml字符串 echo $xml.'<br/>';
$a=wddx_deserialize($xml);
//将xml字符串反序列化为对象数组
foreach ($a as $aa) { echo $aa->name.'<br/>'; }
