JSON in PHP data Interactive instance Tutorial

Source: Internet
Author: User
Tags serialization

How to use JSON. PHP5.2 began with the built-in JSON support. Of course, if lower than this version, then there are many PHP version of the implementation, just the next use on the OK. Now the main is to talk about PHP built-in support for JSON. Very simple, two functions: Json_encode and Json_decode (like serialization). One encoding, one decoding. First look at the use of the code:
? Php
$arr = Array (
' Name ' => ' Chen Yixin ',
' Nick ' => ' Deep Space ',
' Contact ' => Array (
' Email ' => ' shenkong at qq dot com ',
' website ' => ' http://www.111cn.net ',
)
);
$json _string = Json_encode ($arr);
echo $json _string;
? >
It's very simple to have an array of JSON. It should be pointed out that, in the UTF-8 encoding, the Chinese characters will not be encode, the result will come out null value, so if you use gb2312 to write PHP code, then you need to use the content of English to iconv or MB to UTF-8 and then Json_encode, The results of the above output are as follows:
{"Name": "u9648u6bc5u946b", "Nick": "u6df1u7a7a", "Contacts": {"email": "Shenkong at qq dot com", "website": "http://www.111cn.net"}}
I said it's like serialization, and you don't believe it. After encoding to decode, PHP provides the corresponding function Json_decode,json_decode execution, will get an object, the operation is as follows:
? Php
$arr = Array (
' Name ' => ' Chen Yixin ',
' Nick ' => ' Deep Space ',
' Contact ' => Array (
' Email ' => ' shenkong at qq dot com ',
' website ' => ' http://www.111cn.net ',
)
);
$json _string = Json_encode ($arr);
$obj = Json_decode ($json _string);
Print_r ($obj);
? >
Access to properties within an object, right? $obj->name, this way, of course, you can also turn it to an array for easy invocation:
$json _string = Json_encode ($arr);
$obj = Json_decode ($json _string);
$arr = (array) $obj;
Print_r ($arr);
The use of PHP is not particularly large, in addition to cache generation, feel better than directly to save the array, but when you and the front of the interaction, its role is out, below see how I use JAVASCRĪPT to use this paragraph of characters:
<scrīpt type= "Text/javascrīpt" >
var arr = {"name": "u9648u6bc5u946b", "Nick": "u6df1u7a7a", "Contacts": {"email": "Shenkong at qq dot com", "website":http://www.111cn.net}};
Alert (Arr.name)
</scrīpt>
Above, the string is directly assigned to a variable, and it becomes a javascrīpt array (the specialization term should not be called an array, but because of PHP's customary problems, I've been called an array well, easy to understand). This way, you can easily traverse the arr or do whatever you want to do. Writing here, it seems to have not mentioned Ajax Oh? Yes, well, if the server-side returns responsetext with a JSON-like string instead of XML, is the front desk javascrīpt easy to handle? That's how Dogskin plaster is used.
In fact, in addition to the data stored in the format is not the same, JSON and XML is not much different from Oh, but below I say a little. Although it has little to do with XML, it is possible to illustrate the broader application of JSON, that is, cross-domain data invocation. Because of security issues, Ajax does not support Cross-domain calls, so to invoke data under different domain names, it is troublesome, although there is a solution (stone in his lectures on the agent ah what I don't understand but know can solve). I wrote two files, enough to show the Cross-domain call.
Keynote file index.html
<scrīpt type= "Text/javascrīpt" >
function GetProfile (str) {
var arr = str;
document.getElementById (' Nick '). InnerHTML = Arr.nick;
}
</scrīpt>
<body><div id= "Nick" ></div></body>
<scrīpt type= "text/javascrīpt" src= "http://www.openphp.cn/demo/profile.php "></scrīpt>
Profile.php of the document being transferred
? Php
$arr = Array (
' Name ' => ' Chen Yixin ',
' Nick ' => ' Deep Space ',
' Contact ' => Array (
' Email ' => ' shenkong at qq dot com ',
' website ' => ' http://www.111cn.net ',
)
);
$json _string = Json_encode ($arr);
echo "GetProfile ($json _string)";
? >
Obviously, when Index.html calls Profile.php, the JSON string is generated and passed in GetProfile as a parameter, and then the nickname is inserted into the Div, so that a cross-domain data interaction is 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.