The application of JSON in PHP

Source: Internet
Author: User
Tags access properties javascript array
The internet today, Ajax is no longer a strange word. Speaking of Ajax, it's possible to immediately think of XML emerging from RSS. XML parsing, I am afraid, is not a problem, especially PHP5, the emergence of a large number of XML parsers, such as the most lightweight simplexml. However, for Ajax, the parsing of XML is more inclined to the support of the foreground JavaScript. I think all the people who parse the XML will have a big head because of the tree and the node. Admittedly, XML is a very good way to store data, but its flexibility has caused the difficulty of its parsing. Of course, the difficulty referred to here is relative to the protagonist--json of this article.
What is JSON? I will not repeat the concept. In layman's words, it is a storage format for data, just like a string after PHP serialization. It is a data description. For example, when we serialize an array and store it, we can easily deserialize the post-serialization application. The same is true of JSON, except that he builds the interactive bridge between client-side JavaScript and server PHP. We use PHP to generate the JSON string, and then pass the string to the foreground javascript,javascirpt can easily be anti-JSON and then apply. Speaking of popular point, it really looks like an array.
Anyway, how to use JSON. PHP5.2 starts with the JSON support built in. Of course, if it is lower than this version, then there are many implementations of the PHP version on the market, the next use is OK. Now the main is to talk about PHP built-in support for JSON. Very simple, two functions: Json_encode and Json_decode (much like serialization). One encoding, one decoding. First look at the use of encoding:


<?php
$arr = Array (
' Name ' = ' Chen Yixin ',
' Nick ' = ' Deep Space ',
' Contact ' = = Array (
' Email ' = ' shenkong at qq dot com ',
' website ' = ' http://www.chenyixin.com ',
)
);
$json _string = Json_encode ($arr);
echo $json _string;
?>
It's very simple to put an array json. It should be pointed out that in the non-UTF-8 encoding, the Chinese characters will not be encode, the result will be null, so if you use gb2312 to write PHP code, then you need to include Chinese content using Iconv or MB to UTF-8 and then Json_encode, The above output results are as follows:

{"Name": "\u9648\u6bc5\u946b", "Nick": "\u6df1\u7a7a", "contact": {"e-mail": "Shenkong at qq dot com", "website": "Http:\/\ /www.chenyixin.com "}}
I've 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, you will get an object, operation as follows:

<?php
$arr = Array (
' Name ' = ' Chen Yixin ',
' Nick ' = ' Deep Space ',
' Contact ' = = Array (
' Email ' = ' shenkong at qq dot com ',
' website ' = ' http://www.chenyixin.com ',
)
);
$json _string = Json_encode ($arr);
$obj = Json_decode ($json _string);
Print_r ($obj);
?>
Access properties within an object will it? $obj->name, this, of course, can also be transferred to the array, convenient to call:

$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, the feeling is not as good as the direct storage array, but when you interact with the foreground, it is out of the role of it, below to see how I used JavaScript to use this character:

<script type= "Text/javascript" >
var arr = {"name": "\u9648\u6bc5\u946b", "Nick": "\u6df1\u7a7a", "contact": {"e-mail": "Shenkong at qq dot com", "website": " Http:\/\/www.chenyixin.com "}};
Alert (Arr.name)
</script>
Above, directly assign this string to a variable, it becomes a JavaScript array (professional terminology should not be called an array, but because of the habit of PHP, I have been called array good, easy to understand). In this way, you can easily traverse arr or do whatever you want. It doesn't seem to mention Ajax in this case? Yes, Lenovo, if the responsetext returned by the server replaces XML with a JSON string, is it convenient for the foreground JavaScript to be processed? That's how Dogskin plaster is used.
Actually written here, in addition to the data storage format is not the same, JSON and XML is not much different, oh, but I said the next point. Although it does not have much to do with XML, it is possible to describe a wider range of applications for JSON, that is, cross-domain data invocation. Because of security issues, Ajax does not support cross-domain calls, so to invoke different domain names under the data, very troublesome Oh, although there is a solution (stone in his lecture mentioned the agent ah what although not understand but know can solve). I wrote two files that were enough to show cross-domain calls.
Keynote file index.html
<script type= "Text/javascript" >
function GetProfile (str) {
var arr = str;
document.getElementById (' Nick '). InnerHTML = Arr.nick;
}
</script>
<body><div id= "Nick" ></div></body>
<script type= "Text/javascript" src= "http://www.openphp.cn/demo/profile.php" ></script>
Transferred Files profile.php
<?php
$arr = Array (
' Name ' = ' Chen Yixin ',
' Nick ' = ' Deep Space ',
' Contact ' = = Array (
' Email ' = ' shenkong at qq dot com ',
' website ' = ' http://www.chenyixin.com ',
)
);
$json _string = Json_encode ($arr);
echo "GetProfile ($json _string)";
?>
Obviously, when Index.html calls Profile.php, the JSON string is generated and passed in as a parameter to the GetProfile, and the nickname is inserted into the div so that the cross-domain data interaction is complete.
  • 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.