PHP serialize and JSON Parsing

Source: Internet
Author: User

JSON is based on a subset of JavaScript Programming Language, Standard ECMA-262 3rd Edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language.

JSON is constructed in two structures:

A collection of name/value pairs ). In different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list or associative array ).

An ordered list of values ). In most languages, it is understood as an array ).

PHP serialize serializes variables and returns a string expression with the variable type and structure,
Both represent a Data Structure in the form of a string. What is the difference between them.

Let's start with JSON and look at a simple instance.

Example 1:

 
 
  1. var test = {"Name":"Peter","Age":20};  
  2. document.write(test.Name + ": " + test.Age); 

Display result:

Peter: 20

{"Name": "Peter", "Age": 20} in the test variable is an object with two elements (like an array of PHP ):
The Name is Peter, and the Age is 20.

Of course, it can also become more complex.

Example 2:

 
 
  1. var test = {"User":{"Name":"Peter","Age":20},"Company":"FORD"};  
  2. document.write(test.User.Name + ": " + test.Company); 

Display result:

Peter: FORD in this example, the User element contains the Name and Age.

If you want to embody multiple users, you need to use an array, which is different from the object's "{}", and the array uses "[]".

JSON parsing Example 3:

 
 
  1. VarTest= [
  2. {"User": {"Name": "Peter", "Age": 20}, "Company": "FORD "},
  3. {"User": {"Name": "Li Ming", "Age": 20}, "Company": "Benz "}
  4. ];
  5. Document. write (test [1]. User. Name + ":" + test [1]. Company );
  6. // Or use: document. write (test [1] ["User"] ["Name"] + ":" + test [1] ["Company"]);

JSON parsing result:

Li Ming: Benz

Through the simple example above, some complex data can be transmitted through a string, which is indeed much more convenient to work with Ajax.
Next let's take a look at the role of the PHP serialize function.

JSON parsing Example 4:

 
 
  1. $arr = array 
  2.        (  
  3.           'Peter'=> array  
  4.           (  
  5.             'Country'=>'USA',  
  6.             'Age'=>20  
  7.           ),  
  8.           'Li Ming'=> array  
  9.           (  
  10.              'Country'=>'CHINA',  
  11.              'Age'=>21  
  12.           )  
  13.         );  
  14.  
  15. $serializeserialize_var = serialize($arr);  
  16. echo $serialize_var; 

JSON parsing result:

 
 
  1. a:2:{s:5:"Peter";a:2:{s:7:"Country";s:3:"USA";s:3:"Age";i:20;}s:7:"Li Ming";a:2:{s:7:"Country";s:5:"CHINA";s:3:"Age";i:21;}} 

This result looks more complex than JSON, but it is also very simple. It describes some data types and structures.
Take a: 2: {s: 7: "Country"; s: 3: "USA"; s: 3: "Age"; I: 20;} as an example:

A: 2 indicates that this is an array (array) with two elements, s: 7: "Country"; s: 3: "USA"; is the first element, s: 7 indicates that this is a 7-character string (string) followed by I: 20; it should also be guessed as an integer (integer) 20.

Let's take a look at this example,

Example 5:

 
 
  1. class test  
  2. {  
  3.     var $var = 0;  
  4.     function add(){  
  5.       echo $var+10;  
  6.     }  
  7. }  
  8.  
  9. $unserialize_var = new test;  
  10. $serialize_var = serialize($unserialize_var);  
  11. echo $serialize_var;  
  12. $unserialize_var = null;  
  13. $unserialize_var = unserialize($serialize_var);  
  14. $unserialize_var->add(); 

Display result:

O: 4: "test": 1: {s: 3: "var"; I: 0 ;}

10

In this example, we can see that serialize saves the data type and structure,
The add () method can still be used for unserialize variables.

Are there any contacts between PHP and JSON? If you are familiar with PHP, you should know that PHP5.2.0 has set JSON extension as the default component. That is to say, we can perform JSON operations in PHP, the functions are json_encode and json_decode.

Example 6:

 
 
  1. $arr = array 
  2.        (  
  3.           'Name'=>'Peter',  
  4.           'Age'=>20  
  5.        );  
  6.  
  7. $jsonencode = json_encode($arr);  
  8. echo $jsonencode; 


Display result:

{"Name": "Peter", "Age": 20}

The result is the same as the test value in the example. The json_encode is used to convert the variable in PHP into a JSON character expression.
Let's take a look at the usage of json_decode.

Example 7:

 
 
  1. $var = '{"Name":"Peter","Age":20}';  
  2. $jsondecode = json_decode($var);  
  3. print_r($jsondecode); 

Display result:

 
 
  1. stdClass Object ( [Name] => Peter [Age] => 20 ) 

This does verify that {"Name": "Peter", "Age": 20} In JSON is an object, but it can also be converted into an array in PHP, set the ASSOC parameter to True in json_decode.
Example 8:

 
 
  1. $var = '{"Name":"Peter","Age":20}';  
  2. $jsondecode = json_decode($var,true);  
  3. print_r($jsondecode); 

Display result:

 
 
  1. Array ( [Name] => Peter [Age] => 20 ) 

In addition, it should be noted that JSON is based on Unicode format, so to perform a Chinese operation to convert it to UTF-8 format. Through the above examples, we believe that you have a preliminary understanding of JSON and PHP serialize and json_encode. Combined with PHP, Javascript, JSON, and Ajax, you can complete powerful data interaction functions.


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.