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:
- var test = {"Name":"Peter","Age":20};
- 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:
- var test = {"User":{"Name":"Peter","Age":20},"Company":"FORD"};
- 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:
- VarTest= [
- {"User": {"Name": "Peter", "Age": 20}, "Company": "FORD "},
- {"User": {"Name": "Li Ming", "Age": 20}, "Company": "Benz "}
- ];
- Document. write (test [1]. User. Name + ":" + test [1]. Company );
- // 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:
- $arr = array
- (
- 'Peter'=> array
- (
- 'Country'=>'USA',
- 'Age'=>20
- ),
- 'Li Ming'=> array
- (
- 'Country'=>'CHINA',
- 'Age'=>21
- )
- );
-
- $serializeserialize_var = serialize($arr);
- echo $serialize_var;
JSON parsing result:
- 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:
- class test
- {
- var $var = 0;
- function add(){
- echo $var+10;
- }
- }
-
- $unserialize_var = new test;
- $serialize_var = serialize($unserialize_var);
- echo $serialize_var;
- $unserialize_var = null;
- $unserialize_var = unserialize($serialize_var);
- $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:
- $arr = array
- (
- 'Name'=>'Peter',
- 'Age'=>20
- );
-
- $jsonencode = json_encode($arr);
- 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:
- $var = '{"Name":"Peter","Age":20}';
- $jsondecode = json_decode($var);
- print_r($jsondecode);
Display result:
- 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:
- $var = '{"Name":"Peter","Age":20}';
- $jsondecode = json_decode($var,true);
- print_r($jsondecode);
Display result:
- 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.