You may have no longer strange to the PHP, then the understanding of JSON then? Today to tell you a brief introduction to using JSON PHP specific code, I hope this article has a friend in need of help.
Today, the Internet, AJAX is not a strange vocabulary.Speaking of AJAX, RSS may immediately think of because of the rise of XML.XML parsing, I am afraid is not a problem, especially PHP5, the emergence of a large number of XML parser, such as the lightweight SimpleXML.But for the AJAX, XML parsing is more inclined to support Javascript, front desk.I think all people who parsed XML will head for the big tree and node.Denied, XML is a very good way of data storage, but the flexibility is precisely the difficulties caused by its resolution.Of course, the difficulties referred to here, is relative to the protagonist of this article - JSON concerned.
What Is JSON? I will not repeat the concept.Popular that it is a data storage format, just as PHP serialized strings.It is a description of the data.For example, we will be a serialized array of storage, can be easily applied after deserialization.JSON is also true, but he has built a client-side Javascript and PHP to interact with the bridge service.After we use the PHP JSON generated string, and then this string to front Javascript, Javascirpt can easily be applied against JSON and then.Popular point that it really like array.
Closer to home, how to PHP using JSON.PHP5.2 started JSON support built.Of course, if less than this version, then the market realized there are many versions of PHP, casually next to OK with friends.PHP is mainly talk about the built-in support for JSON.Very simple, two functions: json_encode and json_decode (with the sequence of like you).A code, a decoding.Look at the code to use:
'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;?>
An array of very simple JSON out.It should be noted that in non-UTF-8 encoding, the Chinese characters will not be encode, the results will come out empty values, so if you use gb2312 write PHP code, you need to include the contents of the Chinese into use iconv or mbUTF-8 further json_encode, the above output as follows:
{"Name": "u9648u6bc5u946b", "nick": "u6df1u7a7a", "contact": {"email": "shenkong at qq dot com", "website": "
I told you and serialization like, you do not believe.Encoded will decode, PHP provides a corresponding function json_decode, json_decode executed, will be an object, 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 to properties within the object, right?$ Obj-> name, like this, of course, you can turn it the median group, easy call then:
$ Json_string = json_encode ($ arr);
$ Obj = json_decode ($ json_string);
$ Arr = (array) $ obj;
print_r ($ arr);
Above, you will json_encode wrap the string in parentheses, in the implementation of eval, it becomes a Javascript array of (array of specialized terminology should not be called, but because the habit of PHP, I have been called an array of good, easy tounderstanding).This can easily be traversed on arr or any thing you want to do.I write to you, if not mentioned AJAX Oh?Is oh, think about the, if the server returns a string responseText instead of using JSON over XML if Javascript handling up front is not very convenient?Sticking plaster is so used.
In fact, I write to you, in addition to the data storage format is not the same outside, JSON and XML are not much different from Oh, but here I said that.Although XML does not matter much, however, can explain a wider range of application of JSON, that is, cross-domain data calls.Because of security issues, AJAX does not support cross-domain call, so to call the data under different domain names, a lot of trouble Oh, though there is a solution (stone in his lecture mentioned ah, what the agent did not understand but know thoughcan solve).I wrote two documents sufficient to show cross-domain calls.
Melody file index.html
Translate to