PHP Serialize & JSON parsing

Source: Internet
Author: User
Tags ming php json

For JSON (JavaScript Object Notation) Everyone should be familiar, it is a lightweight data interchange format. Easy for people to read and write. It is also easy for machine parsing and generation. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

JSON is constructed in two structures:

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

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


PHP's serialize is to serialize variables, return a string expression with variable type and structure,
It is said that both are in a string way to embody a data structure, then what is the difference between them?

Let's start with JSON and look at a simple example.
Example One :

VarTest= { "name ":" peter "," age ":20};
document.write (test. Name +  " Span style= "color: #000000;" >:  " +< Span style= "color: #000000;" > test.

Show Results:

Peter:

The variable test {"Name": "Peter", "Age": 20} is an object with 2 elements (it feels like an array of PHP):
The name is Peter,age 20.

Of course, it can become more complicated.
Example Two :

VarTest={"User":{"Name":"Peter "," age ":20}, "company" : "ford" };
document.write (test. User.name +  ": "  + test.

Show Results:

Peter:ford

In this example, the user element contains name and age.

If you want to embody more than one user, you need to use an array, which is different from the object's "{}", and the array uses "[]".
example Three :

VarTest=[
{"User":{"Name":"Peter","Age":20},"Company":"FORD"},
{"User":{"Name":"Li Ming","Age":20},"Company":benz ""
               ];
document.write (Test[1+  ": "  + test[1 ]. Company);
//

Show Results:

Li Ming:benz


With the simple example above, it is easy to pass some complex data through a string, and then it is much more convenient to work with Ajax.
Let's look at the role of PHP's Serialize function.


With the simple example above, it is easy to pass some complex data through a string, and then it is much more convenient to work with Ajax.
Let's look at the role of PHP's Serialize function.
example Four:=Array
(
‘Peter‘=Array
(
‘Country‘=‘USA‘,
‘Age‘=20
),
‘Li Ming‘=Array
(
‘Country‘=‘China‘,
               ' age "=>21
           )
        );

$serialize _var  = serialize< Span style= "color: #000000;" > ( $arr echo $ Serialize_var

Show Results:

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

The result looks a bit more complicated than JSON, but it's also very simple, and it shows some data types and structures.
To A:2:{s:7: "Country"; S:3: "USA"; s:3: "Age"; i:20;} For example:
A:2 indicates that this is an array with two elements,
s:7: "Country"; S:3: "USA"; for the first element, S:7 explains that this is a 7-character string,
The back i:20; it should also be guessed that it is an integer 20.

As can be seen from this example, serialize the type and structure of the data to be preserved,
The unserialize variable can still use the Add () method.

So PHP and JSON have no connection, the familiar PHP friends should understand that PHP5.2.0 has set the JSON extension as the default component, that is, we can do json in PHP, its functions are Json_encode and Json_decode.
example Six :

$arr=Array
(
‘Name‘= ' peter ,
           '

Show Results:

{"Name":"Peter","Age":

The result is the same as the test value in example one, which converts a variable in PHP to a JSON character-out expression by Json_encode.
Let's look at the usage of Json_decode.
Example VII :

$var = '{' Name ': ' Peter ', ' age ':';
$jsondecode = json_decode ($var);
print_r($jsondecode);

Show Results:

stdClass Object ([Name] = Peter [age] = + )

This does verify that in JSON {"Name": "Peter", "Age": 20} is an object, but you can also convert it to an array in PHP, and set the assoc parameter to true in Json_decode.
example Eight :

$var = '{' Name ': ' Peter ', ' age ':';
$jsondecode = json_decode ($var,true);
print_r($jsondecode);

Show Results:

Array ([Name] = Peter [age] = + )


Also, it is important to note that JSON is based on Unicode format, so it is necessary to convert it to UTF-8 format for Chinese operations.
Through the above examples, I believe that we have a preliminary understanding of JSON and PHP serialize, Json_encode,
Combine PHP, Javascript, JSON, and Ajax to accomplish powerful data interactivity.

Related references:
PHP JSON functions:http://cn.php.net/manual/en/ref.json.php
JSON introduction:http://www.json.org/json-zh.html
NET Friend Example: http://www.only4.cn/archives/95
Http://www.openphp.cn/blog.php?blog_id=12

PHP Serialize & JSON parsing

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.