Home >Backend Development >PHP Tutorial >Application skills of JSON in PHP, PHPJSON application skills_PHP tutorial

Application skills of JSON in PHP, PHPJSON application skills_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:07:39776browse

JSON application skills in PHP, PHPJSON application skills

In today’s Internet, AJAX is no longer an unfamiliar vocabulary. Speaking of AJAX, XML that emerged due to RSS may immediately come to mind. 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 - JSON.
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 a 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.
Closer to home, how to use JSON. 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 take a look at the use of encoding first:

<&#63;php  
$arr = array(  
  'name' => '陈毅鑫',  
  'nick' => '深空',  
  'contact' => array(  
    'email' => 'shenkong at qq dot com',  
    'website' => 'http://www.chinaz.com',  
  )  
);  
$json_string = json_encode($arr);  
echo $json_string;  
&#63;> 

It is very simple to JSON an array. It should be pointed out that under 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 results are as follows:

{"name":"\u9648\u6bc5\u946b","nick":"\u6df1\u7a7a", 
"contact":{"email":"shenkong at qq dot com","website":"http:\/\/www.chinaz.com"}}  

I told you 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:

<&#63;php  
$arr = array(  
  'name' => '陈毅鑫',  
  'nick' => '深空',  
  'contact' => array(  
    'email' => 'shenkong at qq dot com',  
    'website' => 'http://www.chinaz.com',  
  )  
);  
$json_string = json_encode($arr);  
$obj = json_decode($json_string);  
print_r($obj);  
&#63;> 

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

$json_string = json_encode($arr);  
$obj = json_decode($json_string);  
$arr = array($obj);  
print_r($arr);  

PHP is not particularly useful for moving around. In addition to cache generation, it feels like directly storing an array. However, when you interact with the front desk, its role comes out. Let’s see how I use Javascript. To use this character:

<script type="text/javascript"> 
var arr = {"name":"\u9648\u6bc5\u946b","nick":"\u6df1\u7a7a", 
"contact":{"email":"shenkong at qq dot com","website":"http:\/\/www.chinaz.com"}};  
alert(arr.name)  
</script> 

In the above, directly assign this string to a variable, and it will become a Javascript array (the professional term should not be called an array, but due to PHP's habits, I will 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 point below. Although it has little to do with XML, it can illustrate the wider application of JSON, 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.
Main file index.html

<script type="text/javascript">  
function getProfile(str) {  
  var arr = str;  
  document.getElementById('nick').innerHTML = arr.nick;  
}  
</script>  
<body><div id="nick"></div></body>  
<script type="text/javascript" src="http://www.openphp.cn/demo/profile.php"></script>  

The adjusted file profile.php

<&#63;php  
$arr = array(  
  'name' => '陈毅鑫',  
  'nick' => '深空',  
  'contact' => array(  
    'email' => 'shenkong at qq dot com',  
    'website' => 'http://www.chinaz.com',  
  )  
);  
$json_string = json_encode($arr);  
echo "getProfile($json_string)";  
&#63;> 

Obviously, when index.html calls profile.php, a JSON string is generated and passed to getProfile as a parameter, and then the nickname is inserted into the div. In this way, a cross-domain data interaction is completed. Isn’t it very simple? .

Since JSON is so simple and easy to use, what are you waiting for? I hope this article will be helpful to everyone's study.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1060101.htmlTechArticleJSON application skills in PHP, PHPJSON application skills In today's Internet, AJAX is no longer an unfamiliar vocabulary. Speaking of AJAX, XML that emerged due to RSS may immediately come to mind. Undeniable...
Statement:
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