JSON format and serialize serialization, jsonserialize

Source: Internet
Author: User

JSON format and serialize serialization, jsonserialize
I. JSON formatting

1. What is JSON?

JSON is a data storage format used to communicate the interaction between client Javascript and server PHP. We can transmit the JSON string generated in PHP to the front-end Javascript, and compress cirpt can easily apply the JSON string.

2. How to Use JSON

You can use json_encode () and json_decode () functions for JSON operations in PHP-One encoding and one decoding. Json_encode () can convert an array into json text data for easy storage and reading, while json_decode () can directly convert json data into an array for easy calling.

<? Php $ arr = array ('name' = 'Liu Lu', 'Nick '= 'lu Xiaolu', 'age' = '26 ', '''contact '= array ('phone' = '000000', 'address' = 'Beijing ifdoo'); $ str = json_encode ($ arr ); echo "getProfile ($ str)";?>

Result:
{"Name": "\ u5218 \ u7490", "nick": "\ u7490 \ u5c0f \ u7490", "age": "26", "contact ": {"phone": "13718136109", "address": "\ u5317 \ u4eac \ u5f97 \ u8c46 "}}

3. Asynchronous interaction between JSON-format data and WEB Front-end JS

After PHP converts an array into json data using json_encode (), this json string is equivalent to an array in JavaScript. After being assigned to a variable, you can operate on this array.

<script type="text/javascript>    var arr = {"name":"\u5218\u7490","nick":"\u7490\u5c0f\u7490","age":"26","contact":{"phone":"13718136109","address":"\u5317\u4eac \u5f97\u8c46"}};    alert(arr.name);  </script>  

4. Instance

Index.html

Profile. php

<? Php $ arr = array ('name' => 'Liu Lu', 'Nick '=> 'lu Xiaolu', 'age' => '26 ', 'Contact '=> array ('phone' => '123456', 'address' => 'Beijing ifdoo'); $ str = json_encode ($ arr ); echo "getProfile ($ str)";?>
  • Html page calls PHP File

    <script language="text/javascript" src="/xx/a.php"></script>

    Echo in a. php outputs javascript code.

  • Php page calls js File

    Method in echo js in a. php.

Ii. serialize serialization

1. What is serialize?

Serialize serializes a variable and returns a string expression with the variable type and structure.

2. How to Use serialize

Use PHP serialize and unserialize to serialize and deserialize arrays.

<? Php $ arr = array ("u1" => array ("gameName" => "Deb", "homeName" => "Pretoria ", "guestName" => "Brunswick", "endTime" => "2015-08-21"), "u2" => array ("gameName" => "Premier League ", "homeName" => "", "guestName" => "Aston Villa", "endTime" => "2015-08-22"); echo serialize ($ arr);?>

Result:

A: 2: {s: 2: "u1"; a: 4: {s: 8: "gameName"; s: 6: "Deb"; s: 8: "homeName"; s: 15: "Pretoria"; s: 9: "guestName"; s: 12: "Brunswick"; s: 7: "endTime"; s: 10: "2015-08-21";} s: 2: "u2"; a: 4: {s: 8: "gameName"; s: 6: "Premier League"; s: 8: "homeName"; s: 9: ""; s: 9: "guestName"; s: 15: "Aston Villa"; s: 7: "endTime"; s: 10: "2015-08-22 ";}}

Where:

A: 2 It indicates that this is an array with two elements (array );
I: 0 indicates a sequence index;
A: 4 indicates four fields;
S: 8: "gameName" indicates that this is a string with 8 characters)

Summary:PHP serialize makes it easy to store serialized arrays, while JSON-format data is not only easy to store but also can be read from other languages such as javascript. If the frontend and backend interactions are relatively large, JSON is recommended. Combined with PHP, Javascript, JSON, and Ajax, powerful data interaction functions can be completed.

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.