JSON formatting and serialization of serialize

Source: Internet
Author: User

First, JSON format

1. What is JSON

JSON is a data storage format used to communicate client-side JavaScript and server PHP interactions. We pass the JSON-generated string to the foreground javascript,javascirpt, which we can easily reverse-json and then apply.

2. How to use JSON

PHP operation JSON can use Json_encode () and Json_decode () two functions--one encoding, one decoding. Json_encode () can convert arrays into JSON-formatted text data for easy storage and reading, while Json_decode () can directly convert JSON data into arrays for easy invocation.

<?php    $arr=Array(' name '=' Liu Lu ',' Nick '=' Little Lulu ',' age '=' + ',``' Contact '=Array(' phone '=' 13718136109 ',' Address '=' Beijing Ifdoo ')    );$str= Json_encode ($arr);Echo "GetProfile ($str)";?>

The result is:
{"Name": "\u5218\u7490", "Nick": "\u7490\u5c0f\u7490", "Age": "+", "contact": {"Phone": "13718136109", "address": "\ U5317\u4eac \u5f97\u8c46 "}}

3. JSON-formatted data and web front-end JS complete asynchronous interaction process

After PHP uses Json_encode () to convert an array to JSON-formatted data, this JSON string is equivalent to an array in JavaScript, which is assigned to a variable and can be manipulated by the array.

<script Type= "Text/javascript>var arr = {"Name":"\u5218\u7490","Nick.":"\u7490\u5c0f\u7490","Age":"a","Contact":{"Phone":"13718136109","Address":"\u5317\u4eac \u5f97\u8c46"}};  alert (arr.name); </script>

4. Example

Index.html

<html><head>    <title>JSON demo</title>    <script type="Text/javascript">  function getprofile(str) { var arr = str;    document. getElementById (' Nick '). InnerHTML = Arr.nick; }        </script></head><body>    <div id="Nick"></div></body><script type="text/javascript" src="./profile.php"></script></html>

profile.php

<?php    $arr=Array(' name '=' Liu Lu ',' Nick '=' Little Lulu ',' age '=' + ',' Contact '=Array(' phone '=' 13718136109 ',' Address '=' Beijing Ifdoo ')    );$str= Json_encode ($arr);Echo "GetProfile ($str)";?>
  • HTML page called PHP file

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

    The echo output in a.php is the JavaScript code.

  • PHP page Call js file

    A.php in the Echo JS method can be.

II. serialization of Serialize

1. What is serialize?

Serialize is a string expression that serializes a variable and returns a variable type and structure.

2. How to use Serialize

Use PHP's serialize and unserialize to serialize and deserialize arrays.

<?php    $arr=Array("U1"=Array("Gamename"="de-B","Homename"="Bielefeld","Guestname"="Braunschweig","EndTime"="2015-08-21"),"U2"=Array("Gamename"="Premier League","Homename"="Crystal Palace","Guestname"="Aston Villa","EndTime"="2015-08-22")     );EchoSerialize$arr);?>

The result is:

A:2:{s:2: "U1"; A:4:{s:8: "Gamename"; s:6: "de-B"; S:8: "Homename"; s:15: "Bielefeld"; S:9: "Guestname"; s:12: "Braunschweig"; 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: "Crystal Palace"; S:9: "Guestname"; s:15: "Aston Villa"; s:7: "EndTime"; s:10 : "2015-08-22";}}

which

A:2 indicates that this is an array with two elements;
I:0 refers to the sequential index;
A:4 means there are 4 fields;
S:8: "Gamename" indicates that this is a string with 8 characters (string)

Summary: PHP's serialize is easy to store after serializing arrays, while JSON-formatted data is not only easy to store but also readable with other languages such as JavaScript. It is recommended to use JSON if the front-end interaction is used more, with PHP, Javascript, JSON, and Ajax to accomplish powerful data interactivity.

JSON formatting and serialization of serialize

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.