Summary of typical cases of php processing json format data, and typical cases of json. The typical cases of php processing json data are summarized. the example in this article summarizes the methods of php processing json data. I would like to share it with you for your reference. The details are as follows: 1. Summary of typical cases of php processing json format data
This example summarizes how php processes json data. We will share this with you for your reference. The details are as follows:
1. json introduction:
What is json?
Simply put, JSON can convert a set of data represented in a JavaScript object to a string, and then it can be easily passed between functions, alternatively, the string is transmitted from the Web client to the server in an asynchronous application.
In layman's terms, it is a data storage format, just like a php serialized string.
It is also a data description. for example, if we store an array after serialization, it can be easily deserialized and applied. json is also the case, it builds a bridge between client javascript and server php.
How to use json?
Versions starting from php5.2 and later have built-in json support, mainly including two functions:
Json_encode (): encoded to generate a json string
Json_decode (): a decoder
Note:After json_encode () function encoding, a json string is returned, for example, $ json = '{"a": 1, "B": 2, "c ": 3, "d": 4, "e": 5} '; output a string in json format and a javascript Object in json format will be obtained.
2. json Case 1:
Use of json_encode:
<? Php $ arr = array ('name' => 'Wei Yanhui ', 'Nick' =>, 'Contact '=> array ('email' => 'zhuoweida @ 163.com ', 'Website' => 'http: // zhuoweida.blog.tianya.cn ',)); $ json_string = json_encode ($ arr); echo $ json_string; // string in json format?>
Result:
{ "name":"\u9648\u6bc5\u946b", "nick":"\u6df1\u7a7a", "contact": { "email":"shenkong at qq dot com", "website":"http:\/\/www.chinaz.com" }}
Tip:The output data itself is a js object in json format. because there is no quotation mark, you can directly use it as a json object on the front-end page.
Summary:The correlated array is constructed according to the javascript object.
Analysis: In the above case, an array is converted into json. it should be noted that in non-UTF-8 encoding, Chinese characters cannot be encoded and the result will be null, therefore, if you use gb2312 encoding to write php code, you need to convert the content that contains Chinese characters into UTF-8 using iconv or mb functions
3. json Case 2:
Use of json_decode:
<? Php $ arr = array ('name' => 'Wei Yanhui ', 'Nick' => 'dream soar ', 'Contact '=> array ('email' => 'zhuoweida @ 163.com ', 'Website' => 'http: // zhuoweida.blog.tianya.cn ',)); $ json_string = json_encode ($ arr); $ obj = json_decode ($ json_string); // you can use $ obj-> name to access the attributes of the object $ arr = json_decode ($ json_string, true); // when the second parameter is set to true, it will be converted to the array print_r ($ obj); print_r ($ arr);?>
Result:
{ "name":"\u9648\u6bc5\u946b", "nick":"\u6df1\u7a7a", "contact": { "email":"shenkong at qq dot com", "website":"http:\/\/www.chinaz.com" }}
Summary:The correlated array is constructed according to the JavaScript object.
Tip:The output data itself is a js object in json format. because there is no quotation mark, you can directly use it as a json object on the front-end page.
Analysis:Decoding is required after Encoding. php provides the corresponding function json_decode. after executing this function, an object or array will be obtained.
4. json Case 3:
When interacting with the foreground, the role of json is displayed:
For example, the javascript code is as follows: