JSON format and serialize serialization, jsonserialize
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 = Span class= "Hljs-keyword" >array ( ' name ' = ' Liu Lu ' /span>, ' Nick ' = ' Little Lulu ' , ' age ' =" , " ' contact ' =array ( ' phone ' = ' 1 3718136109 ' , ' 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.
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 ( = , ' nick ' = , ' age ' = ' 26 ' /span>, ' contact ' = array ( ' phone ' = ' 13718136109 ' , = ' Beijing Ifdoo ' ); $str = Json_encode ( $arr ); echo "GetProfile ($STR)" ; ?>
-
HTML page called PHP file
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.
$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.
http://www.bkjia.com/PHPjc/1125519.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125519.html techarticle JSON format and serialize serialization, Jsonserialize, JSON format 1. JSON is what JSON is a data storage format used to communicate client JavaScript and server-side PHP interactions. ...