PHPserialize and JSON parser-PHP Tutorial

Source: Internet
Author: User
Details about PHPserialize and JSON parsing. JSON is a subset of the StandardECMA-2623rdEdition-December1999 based on JavaScriptProgrammingLanguage. JSON uses a Language-independent text format, but also uses a subset similar to JSON based 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. $serializeserialize_var = serialize($arr);
  15. 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. $unserialize_var = new test;
  9. $serialize_var = serialize($unserialize_var);
  10. echo $serialize_var;
  11. $unserialize_var = null;
  12. $unserialize_var = unserialize($serialize_var);
  13. $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. $jsonencode = json_encode($arr);
  7. 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.


Extends Programming Language, a subset of Standard ECMA-262 3rd Edition-December 1999. JSON uses a language-independent text format, but it also uses a format similar...

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.