Correctly Master PHP JSON Application_PHP Tutorial

WBOY
Release: 2016-07-15 13:32:49
Original
714 people have browsed it

I think everyone who has parsed XML will be confused by trees and nodes. It is undeniable that XML is a very good data storage method, but its flexibility makes it difficult to parse. Of course, the difficulty referred to here is relative to the protagonist of this article - PHP JSON application.

What is JSON? I won't repeat the concept. In layman's terms, it is a data storage format, just like a PHP serialized string. It is a data description. For example, if we serialize an array and store it, it can be easily deserialized and applied. The same is true for JSON, except that it builds an interactive bridge between client-side Javascript and server-side PHP. We use PHP to generate the JSON string, and then pass this string to the front-end Javascript. Javascirpt can easily convert it into JSON and then apply it. To put it simply, it really looks like an array.

Back to business, how to correctly master PHP JSON application. PHP5.2 has built-in support for JSON. Of course, if it is lower than this version, there are many PHP version implementations on the market, just use whichever one you want. Now we mainly talk about the JSON built-in support of PHP. Very simple, two functions: json_encode and json_decode (very similar to serialization). One for encoding and one for decoding. Let’s look at the use of encoding first:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">arr</span><span> = </span><span class="attribute-value">array</span><span>(   </span></span></li>
<li>
<span>'name' =</span><span class="tag">></span><span>'陈毅鑫',   </span>
</li>
<li class="alt">
<span>'nick' =</span><span class="tag">></span><span> '深空',   </span>
</li>
<li>
<span>'contact' =</span><span class="tag">></span><span> array(   </span>
</li>
<li class="alt">
<span>'email' =</span><span class="tag">></span><span> 'shenkong at qq dot com',   </span>
</li>
<li>
<span>'website' =</span><span class="tag">></span><span> <br>'http://www.chenyixin.com',   </span>
</li>
<li class="alt"><span>)   </span></li>
<li><span>);   </span></li>
<li class="alt">
<span>$</span><span class="attribute">json_string</span><span> = </span><span class="attribute-value">json_encode</span><span>($arr);   </span>
</li>
<li><span>echo $json_string;   </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

It’s very simple to JSON an array. It should be pointed out that in non-UTF-8 encoding, Chinese characters cannot be encoded, and the result will be null. Therefore, if you use gb2312 to write PHP code, you need to use iconv or mb to convert the content containing Chinese to UTF-8 is then json_encoded, and the above output result is as follows:

<ol class="dp-xml"><li class="alt"><span><span>{"name":"u9648u6bc5u946b"<br>,"nick":"u6df1u7a7a","<br>contact":{"email":"shenkong <br>at qq dot com","website":<br>"http://www.chenyixin.com"}}  </span></span></li></ol>
Copy after login

I have already said that it is very similar to serialization, but you still don’t believe it. After encoding, it is necessary to decode. PHP provides the corresponding function json_decode. After json_decode is executed, an object will be obtained. The operation is as follows:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">arr</span><span> = </span><span class="attribute-value">array</span><span>(   </span></span></li>
<li>
<span>'name' =</span><span class="tag">></span><span>'陈毅鑫',   </span>
</li>
<li class="alt">
<span>'nick' =</span><span class="tag">></span><span> '深空',   </span>
</li>
<li>
<span>'contact' =</span><span class="tag">></span><span> array(   </span>
</li>
<li class="alt">
<span>'email' =</span><span class="tag">></span><span> 'shenkong<br> at qq dot com',   </span>
</li>
<li>
<span>'website' =</span><span class="tag">><br></span><span> 'http://www.chenyixin.com',   </span>
</li>
<li class="alt"><span>)   </span></li>
<li><span>);   </span></li>
<li class="alt">
<span>$</span><span class="attribute">json_string</span><span> = <br></span><span class="attribute-value">json_encode</span><span>($arr);   </span>
</li>
<li>
<span>$</span><span class="attribute">obj</span><span> = </span><span class="attribute-value">json_decode</span><span>($json_string);   </span>
</li>
<li class="alt"><span>print_r($obj);   </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

Can you access the attributes in the object? $obj->name, like this, of course, you can also convert it into an array for easy calling:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">json_string</span><span> = </span><span class="attribute-value">json_encode</span><span>($arr);   </span></span></li>
<li>
<span>$</span><span class="attribute">obj</span><span> = </span><span class="attribute-value">json_decode</span><span>($json_string);   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">arr</span><span> = (array) $obj;   </span>
</li>
<li><span>print_r($arr);  </span></li>
</ol>
Copy after login

PHP is not particularly useful for transferring it around, except for cache generation. It’s better to store the array directly. However, its effect will come out when you interact with the front desk. Let’s see how I use Javascript to use this character:

In the above, directly change this character When a string is assigned to a variable, it becomes a Javascript array (the technical terminology should not be called an array, but due to PHP's habits, I always call it an array for easier understanding). In this way, you can easily traverse arr or do whatever you want. I haven’t mentioned AJAX yet, right? Yes, think about it, if the responseText returned by the server uses a JSON string instead of XML, wouldn't it be very convenient for the front-end Javascript to process it? This is how dog skin plaster is used.

In fact, as I write this, except for the different data storage formats, there is not much difference between JSON and XML, but I will mention one thing below. Although it has little to do with XML, it can be shown that PHP JSON applications have a wider range of applications, that is, cross-domain data calls. Due to security issues, AJAX does not support cross-domain calls. It is very troublesome to call data under different domain names. Although there are solutions (Stone mentioned proxies in his lecture, I don’t understand it but I know it) can be solved). I wrote two files, which are enough to demonstrate cross-domain calls.

The main file index.html

The adjusted file profile.php

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">arr</span><span> = </span><span class="attribute-value">array</span><span>(   </span></span></li>
<li>
<span>'name' =</span><span class="tag">></span><span> '陈毅鑫',   </span>
</li>
<li class="alt">
<span>'nick' =</span><span class="tag">></span><span> '深空',   </span>
</li>
<li>
<span>'contact' =</span><span class="tag">></span><span> array(   </span>
</li>
<li class="alt">
<span>'email' =</span><span class="tag">></span><span> <br>'shenkong at qq dot com',   </span>
</li>
<li>
<span>'website' =</span><span class="tag">></span><span> <br>'http://www.chenyixin.com',   </span>
</li>
<li class="alt"><span>)   </span></li>
<li><span>);   </span></li>
<li class="alt">
<span>$</span><span class="attribute">json_string</span><span> = </span><span class="attribute-value">json_encode</span><span>($arr);   </span>
</li>
<li><span>echo "getProfile($json_string)";   </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

Obviously, when index.html calls profile.php, a JSON string is generated , and pass in getProfile as a parameter, and then insert the nickname into the div. In this way, a cross-domain data interaction is completed. Isn't it very simple? Since the PHP JSON application is so simple and easy to use, what are you waiting for?


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446103.htmlTechArticleI think everyone who has parsed XML will be confused by trees and nodes. It is undeniable that XML is a very good data storage method, but its flexibility makes it difficult to parse. Of course, this...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!