JSON application in PHP

Source: Internet
Author: User
Tags xml parser javascript array

Today, Ajax is no stranger to the Internet. When talking about Ajax, you may immediately think of XML that has emerged due to RSS. XML parsing is no longer a problem, especially PhP5. A large number of XML Parser emerged, such as simplexml, which is the most lightweight. However, for Ajax, XML parsing tends to support JavaScript at the front end. I think all the people who have parsed XML will be headers of trees and nodes. It is undeniable that XML is a good data storage method, but its flexibility has caused difficulties in parsing. Of course, the difficulty mentioned here is relative to JSON, the main character of this article.

What is JSON? I will not repeat the concept. In general, it is a data storage format, just like the serialized string in PHP. It is a data description. For example, if we store an array after serialization, we can easily apply it after deserialization. JSON is the same, but it builds a bridge between client JavaScript and server PHP. We use PHP to generate the JSON string, and then pass the string to the front-end Javascript. Then we can easily apply the javascirpt anti-JSON string. It is really like an array.

To put it bluntly, how to use JSON. Php5.2 has built-in support for JSON. Of course, if it is earlier than this version, there are many PHP versions available on the market, and it will be OK if you use it next time. Now we mainly talk about the built-in JSON supported by PHP. Very simple: two functions: json_encode and json_decode (similar to serialization ). One encoding and one decoding. Let's take a look at the encoding usage:

<? PHP
$ Arr = array (
'Name' => 'chen yixin ',
'Nick '=> 'deep ary ',
'Contact '=> array (
'Email '=> 'shenkong at QQ dot com ',
'Website' => 'HTTP: // www.chinaz.com ',
)
);
$ Json_string = json_encode ($ ARR );
Echo $ json_string;
?>
 

Simply put an array in JSON format. It should be noted that, in non-UTF-8 encoding, Chinese characters will not be encode, the results will be null, So if you use gb2312 to write PHP code, then we need to use iconv or MB to convert the content containing Chinese characters into a UTF-8 and then perform json_encode. The above output result is as follows:

{"Name": "/u9648/u6bc5/u946b", "Nick": "/u6df1/u7a7a", "Contact": {"email ": "shenkong at QQ dot com", "website": "http: // www.chinaz.com "}}

I have said that it is similar to serialization. You still don't believe it. Decoding is required after encoding. php provides the corresponding function json_decode. After json_decode is executed, an object is obtained. The operation is as follows:

<? PHP
$ Arr = array (
'Name' => 'chen yixin ',
'Nick '=> 'deep ary ',
'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 );
?>
 

Will access the attributes in the object? $ Obj-> name. Of course, you can also index the array to facilitate the call:

$ Json_string = json_encode ($ ARR );
$ OBJ = json_decode ($ json_string );
$ Arr = (array) $ OBJ;
Print_r ($ ARR );
 

PHP transfer and transfer are not very useful. In addition to cache generation, it feels better to store arrays directly. However, when you interact with the front-end, the function will come out, next let's take a look at how I use JavaScript to use this character:

<SCRIPT type = "text/JavaScript">
VaR arr = {"name": "/u9648/u6bc5/u946b", "Nick": "/u6df1/u7a7a", "Contact": {"email ": "shenkong at QQ dot com", "website": "http: // www.chinaz.com "}};
Alert (ARR. Name)
</SCRIPT>
 

In the above, the string is directly assigned to a variable, and it becomes a JavaScript Array (specialized terms should not be called arrays, but due to the habits of PHP, I have been calling Arrays for ease of understanding ). In this way, you can easily traverse the ARR or do whatever you want. It seems that AJAX is not mentioned here? Yes. In retrospect, if the responsetext returned by the server uses a JSON string instead of XML, is it very convenient for the front-end JavaScript to process it? Dog Skin plaster is used in this way.

As a matter of fact, apart from the different storage formats of data, there is no big difference between JSON and XML. Although it does not have much to do with XML, it can indicate a wider range of JSON applications, that is, cross-Origin data calls. Due to security issues, Ajax does not support cross-origin calls. Therefore, it is very troublesome to call data under different domain names, although there is a solution (stone mentioned proxy in his lecture. Although he doesn't understand it, he knows it can solve it ). I wrote two files to demonstrate cross-origin calls.

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

<? PHP
$ Arr = array (
'Name' => 'chen yixin ',
'Nick '=> 'deep ary ',
'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 into getprofile as a parameter, and then the nickname is inserted into the Div. This completes a cross-Origin data interaction. Is it very simple. Since JSON is so easy to use and easy to use, what are you waiting?
 

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.