How to use JSON, PHP52 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 for any next use, now we mainly talk about the built-in JSON supported by PHP, which is very simple. There are two functions: json_encode and json_decode.
JSON application method in PHP
How to use JSON, PHP5.2 began to have 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 it is used, now I am mainly talking about the built-in JSON supported by PHP. it is very simple. There are two functions: json_encode and json_decode (similar to serialization), one encoding and one Decoding. let's first look at the encoding usage:
- $ Arr = array (
- 'Name' => 'Chen yixin ',
- 'Nick '=> 'deep ary ',
- 'Contact '=> array (
- 'Email '=> 'shenkong at qq dot com ',
- 'Website' => 'http: // www.phpfensi.com ',
- )
- );
- $ Json_string = json_encode ($ arr );
- Echo $ json_string;
- ?>
Very simple will be an array of JSON, 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, you 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 is as follows:
- {"Name": "u9648u6bc5u946b", "nick": "u6df1u7a7a", "contact": {"email": "shenkong at qq dot com", "website ": "http://www.phpfensi.com "}}
I have mentioned that it is similar to Serialization. if you do not believe it, decoding is required after Encoding. PHP provides the corresponding function json_decode. after json_decode is executed, an object will be 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.phpfensi.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:
- Var arr = {"name": "u9648u6bc5u946b", "nick": "u6df1u7a7a", "contact": {"email": "shenkong at qq dot com ", "website": "http://www.phpfensi.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 always called the array for ease of understanding). In this way, you can easily traverse arr or do whatever you want to do. 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. However, as I mentioned below, although it has little to do with XML, it can be explained that a larger range of applications in JSON, that is, cross-origin data calling, AJAX does not support cross-origin calling due to security issues, so it is very troublesome to call data under different domain names, although there is a solution (stone mentioned proxy in his lecture), I can write two files to demonstrate cross-origin calls.
Access key file index.html
- Function getProfile (str ){
- Var arr = str;
- Document. getElementById ('Nick '). innerHTML = arr. nick;
- }
- Script
- "Nick">
- Script
Profile. php
- $ Arr = array (
- 'Name' => 'Chen yixin ',
- 'Nick '=> 'deep ary ',
- 'Contact '=> array (
- 'Email '=> 'shenkong at qq dot com ',
- 'Website' => 'http: // www.phpfensi.com ',
- )
- );
- $ Json_string = json_encode ($ arr );
- Echo "getProfile ($ json_string )";
- ?>