JSON application method in PHP

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

JSON application method in PHP

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 as soon as the next version is used. 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 Tutorial
$ Arr = array (
'Name' => 'Chen yixin ',
'Nick '=> 'deep ary ',
'Contact '=> array (
'Email '=> 'shenkong at qq dot com ',
'Website' => 'http: // www.111cn.net ',
)
);
$ 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": "u9648u6bc5u946b", "nick": "u6df1u7a7a", "contact": {"email": "shenkong at qq dot com", "website ": "http://www.chenyixin.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.111cn.net ',
)
);
$ 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/webpage effects">
Var arr = {"name": "u9648u6bc5u946b", "nick": "u6df1u7a7a", "contact": {"email": "shenkong at qq dot com ", "website": "http://www..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 server returns responseText with a JSON string instead of XML, will the front-end Javascript processing be very convenient? 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 = "/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.111cn.net ',
)
);
$ Json_string = json_encode ($ arr );
Echo "getProfile ($ json_string )";
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.