PHP Serialize & JSON parsing

Source: Internet
Author: User
Tags 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:

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

Show Results:

Peter:20

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:

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

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:

var test = [
{"User": {"Name": "Peter", "Age": $}, "Company": "FORD"},
{"User": {"Name": "Li Ming", "Age": $}, "Company": "Benz"}
];
document.write (test[1]. User.Name + ":" + test[1]. Company);
or use: document.write (test[1]["User" ["Name"] + ":" + 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.
Example four:

$arr = array
(
' Peter ' = array
(
' Country ' = ' USA ',
' Age ' = 20
) ,
' Li Ming ' = = array
(
' Country ' = ' China ',
' Age ' = 21
)
);

$serialize _var = serialize ($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.

Take another look at this example,
Example five:

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 ();

Show Results:

O:4: "Test": 1: {s:3: "var"; i:0;}
10

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 VI:

$arr = array
(
' Name ' = ' Peter ',
' Age ' = 20
);

$jsonencode = Json_encode ($arr);
Echo $jsonencode;

Show Results:

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

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 ': 20} ';
$jsondecode = Json_decode ($var);
Print_r ($jsondecode);

Show Results:

StdClass Object ([Name] = Peter [age] = 20)

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 VIII:

$var = ' {' Name ': ' Peter ', ' Age ': 20} ';
$jsondecode = Json_decode ($var, true);
Print_r ($jsondecode);

Show Results:

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


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

  • 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.