Application techniques of JSON in PHP _php tips

Source: Internet
Author: User
Tags php code serialization javascript array

The internet today, Ajax is not a strange word. Speaking of Ajax, you may immediately recall the XML that arose as a result of RSS. Admittedly, XML is a very good way to store data, but its flexibility precisely causes the difficulty of its parsing. Of course, the difficulty referred to here is relative to the protagonist--json in this article.
What is JSON? I will not repeat the concept. In layman's parlance, it is a storage format for data, just like a string after a PHP serialization. It is a data description. For example, after we have serialized an array and stored it, we can easily deserialize it and then apply it. The same is true of JSON, except that he builds an interactive bridge between client JavaScript and server PHP. We use PHP to generate the JSON string, and then pass this string to the foreground javascript,javascirpt can easily reverse JSON and then apply it. Speaking of popular point, it really looks like an array.
Anyway, 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.chinaz.com ',  
  )  
;  
$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": "\u9648\u6bc5\u946b", "Nick": "\u6df1\u7a7a", 
"contacts": {"email": "Shenkong at qq dot com", "website": " Http:\/\/www.chinaz.com "}}  

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.chinaz.com ',  
  )  
;  
$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, it is not as good as direct storage array, but when you interact with the foreground, its role is out slightly, below see how I used JavaScript to use this paragraph of characters:

<script type= "Text/javascript" > 
var arr = {"name": "\u9648\u6bc5\u946b", "Nick": "\u6df1\u7a7a", 
" Contacts ": {" email ":" Shenkong at qq dot com "," website ":" Http:\/\/www.chinaz.com "};  
Alert (arr.name)  
</script> 

Above, directly assigning this string to a variable, it becomes a JavaScript array (the specialization term should not be called an array, but because of the habit of PHP, I have been called 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, and if the server-side returns responsetext with a JSON string instead of XML, is it convenient for the foreground JavaScript to handle it? 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

<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>  

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.chinaz.com ',  
  )  
;  
$json _string = Json_encode ($arr);  
echo "GetProfile ($json _string)";  
? > 

Obviously, when Index.html calls Profile.php, the JSON string is generated and passed as a parameter to the GetProfile, and then the nickname is inserted into the Div, so that a cross-domain data interaction is done, and it is not particularly straightforward.

Now that JSON is so simple and easy to use, what are you waiting for? Hope this article is helpful to everyone's study.

Related Article

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.