Today, AJAX is no stranger to PHP and JSON 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 in order, we can easily deserialize it and apply it. 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:
|
$ Arr = array (
'Name' => 'Chen yixin ',
'Nick '=> 'deep ary ',
'Contact '=> array (
'Email '=> 'shenkong at qq dot com ',
'Website' => 'http: // www.itrenjia.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.itrenjia.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:
|
$ Arr = array (
'Name' => 'Chen yixin ',
'Nick '=> 'deep ary ',
'Contact '=> array (
'Email '=> 'shenkong at qq dot com ',
'Website' => 'http: // www.itrenjia.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: