Home > Backend Development > PHP Tutorial > PHP json and xml serialization/deserialization

PHP json and xml serialization/deserialization

coldplay.xixi
Release: 2023-04-09 10:06:02
forward
2371 people have browsed it

PHP json and 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 want to write a jsop Small demo, it turns out that I don’t know how to use php serialization. I checked the information and made a note. Simple array json format serialization/deserialization

php provides json_encode and json_decodeFunction performs json format serialization/deserialization operations on objects

$data=array('Name'=>'Byron','Age'=>24,'Sex'=>'Male','Friends'=>array('Casper','Frank','Vincent')); 
$json=json_encode($data);//将数组序列化为json字符串 echo $json.&#39;<br/>&#39;; 
$array_json= json_decode($json);//将json字符串反序列化为数组 while(list($key,$value)=each($array_json)){ if(!is_array($value)){ echo "$key: $value<br/>"; }else{ echo "$key: "; 
foreach ($value as $current) { echo "$current &emsp;"; } echo &#39;<br/>&#39;; } }
Copy after login

PHP json and xml serialization/deserialization

Simple array xml format serialization/deserialization

php Provide wddx_serialize_value and wddx_deserialize functions to serialize/deserialize objects in xml format

$data=array(&#39;Name&#39;=>&#39;Byron&#39;,&#39;Age&#39;=>24,&#39;Sex&#39;=>&#39;Male&#39;,&#39;Friends&#39;=>array(&#39;Casper&#39;,&#39;Frank&#39;,&#39;Vincent&#39;)); 
$xml=wddx_serialize_value($data);//把数组序列化为xml字符串 echo $xml.&#39;<br/>&#39;; 
$array_xml=wddx_deserialize($xml);//把xml字符串反序列化为数组 while(list($key,$value)=each($array_xml)){ if(!is_array($value)){ echo "$key: $value<br/>"; }else{ echo "$key: "; 
foreach ($value as $current) { echo "$current &emsp;"; } echo &#39;<br/>&#39;; } }
Copy after login

PHP json and xml serialization/deserialization

Although due to HTML transcoding The reason is that the output format is very strange, but in fact the serialized string is like this

PHP json and xml serialization/deserialization

Compared with the json format, there are many more fields
Complex object json Format serialization/deserialization Many times when we operate, the object we process is not a simple array, but an array of our customized objects, json_encode and json_decode It is also competent. Customize an object with similar content to 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; } }
Copy after login
$me1=new Me(&#39;Byron&#39;,24,array(&#39;Casper&#39;,&#39;Frank&#39;,&#39;Vincent&#39;)); 
$me2=new Me(&#39;Casper&#39;,25,array(&#39;Byron&#39;,&#39;Frank&#39;,&#39;Vincent&#39;)); 
$me3=new Me(&#39;Frank&#39;,26,array(&#39;Casper&#39;,&#39;Byron&#39;,&#39;Vincent&#39;)); //创建一个复杂的数组,子元素是自定义类,自定义类中包含数组字段 
$array_me=array($me1,$me2,$me3); 
$json=json_encode($array_me);//序列化对象数组为json字符串 echo $json.&#39;<br/>&#39;; 
$a=json_decode($json);//将json字符串反序列化为对象数组 foreach ($a as $aa) { echo $aa->name.&#39;<br/>&#39;; }
Copy after login

PHP json and xml serialization/deserialization

You can see that the serialized string format is very consistent with expectations. Complex object XML format serialization/deserialization. The same wddx_serialize_value and wddx_deserialize functions are also capable of XML format serialization/deserialization operations on complex objects. Use the object just now as an example

$me1=new Me(&#39;Byron&#39;,24,array(&#39;Casper&#39;,&#39;Frank&#39;,&#39;Vincent&#39;)); $me2=new Me(&#39;Casper&#39;,25,array(&#39;Byron&#39;,&#39;Frank&#39;,&#39;Vincent&#39;)); 
$me3=new Me(&#39;Frank&#39;,26,array(&#39;Casper&#39;,&#39;Byron&#39;,&#39;Vincent&#39;)); //创建一个复杂的数组,子元素是自定义类,自定义类中包含数组字段 
$array_me=array($me1,$me2,$me3); $xml=wddx_serialize_value($array_me);//序列化对象数组为xml字符串 echo $xml.&#39;<br/>&#39;; 
$a=wddx_deserialize($xml);//将xml字符串反序列化为对象数组 foreach ($a as $aa) { echo $aa->name.&#39;<br/>&#39;; }
Copy after login

PHP json and xml serialization/deserialization

The generated xml string structure is like this

PHP json and xml serialization/deserialization
I am a beginner in PHP. There are many fallacies in the article. I hope everyone can criticize and correct me.

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of PHP json and xml serialization/deserialization. For more information, please follow other related articles on the PHP Chinese website!

source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template