You may no longer be unfamiliar with PHP, but what about your understanding of JSON? Today we will briefly introduce to you the specific encoding of JSON used by PHP. We hope that the content of this article will be helpful to friends who need it again.
Today on the Internet, AJAX is no longer an unfamiliar word. Speaking of AJAX, XML that emerged due to RSS may immediately come to mind. XML parsing is probably no longer a problem, especially with PHP5 and the emergence of a large number of XML parsers, such as the most lightweight SimpleXML. However, for AJAX, XML parsing is more inclined to the support of front-end Javascript. 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 - 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 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.
Closer to home, how to use JSON in PHP. 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:
'Chen Yixin', 'nick' => 'deep space', 'contact' => array( 'email' => 'shenkong at qq dot com', 'website' => 'http://www .BkJia.com', ) ); $json_string = json_encode($arr); echo $json_string; ?>
It is 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 results are as follows:
{"name":"u9648u6bc5u946b","nick":"u6df1u7a7a","contact":{"email":"shenkong at qq dot com","website":"
I’ve already told you that it’s 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:
$arr = array(
'name' => 'Chen Yixin',
'nick' => 'Deep Space',
'contact' => array(
'email' => 'shenkong at qq dot com',
'website' => 'http://www.BkJia.com',
)
);
$json_string = json_encode($arr);
$obj = json_decode($json_string);
print_r($obj);
?>
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);
In the above, the json_encode string is wrapped in parentheses. After executing eval, it becomes a Javascript array (the technical term should not be called an array, but due to PHP's habits, I just call it an array for convenience. understand). 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.
其实写到这里,除了数据的存储格式不太一样外,JSON和XML也没什么太大区别哦,不过下面我说的一点。虽然和XML没多大关系,不过,可以说明JSON更大范围的应用,那就是,跨域的数据调用。由于安全性问题,AJAX不支持跨域调用,这样要调用不同域名下的数据,很麻烦哦,虽然有解决方案(stone在他的讲座上提到过了代理啊什么的虽然听不懂但是知道能解决)。我写两个文件,足以展示跨域调用了。
主调文件index.html
The adjusted file profile.php
$arr = array(
'name' => 'Chen Yixin',
'nick' => 'Deep Space',
'contact' => array(
'email' => 'shenkong at qq dot com',
'website' => 'http://www.BkJia.com',
)
);
$json_string = json_encode($arr);
echo "getProfile('($json_string)')";
?>
Obviously, when index.html calls profile.php, a JSON string is generated and passed into getProfile as a parameter, and then the nickname is inserted into the div. In this way, a cross-domain data interaction is completed. Is it particularly simple for PHP to use JSON? .
Maybe everyone is no longer unfamiliar with PHP, but what about your understanding of JSON? Today we will briefly introduce to you the specific encoding of JSON used by PHP. We hope that the content of this article will be helpful to friends who need it again.
Today on the Internet, AJAX is no longer an unfamiliar word. Speaking of AJAX, XML that emerged due to RSS may immediately come to mind. XML parsing is probably no longer a problem, especially with PHP5 and the emergence of a large number of XML parsers, such as the most lightweight SimpleXML. However, for AJAX, XML parsing is more inclined to the support of front-end Javascript. 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 - 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 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.
Closer to home, how to use JSON in PHP. 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:
'Chen Yixin', 'nick' => 'deep space', 'contact' => array( 'email' => 'shenkong at qq dot com', 'website' => 'http://www .BkJia.com', ) ); $json_string = json_encode($arr); echo $json_string; ?>
It is 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 results are as follows:
{"name":"u9648u6bc5u946b","nick":"u6df1u7a7a","contact":{"email":"shenkong at qq dot com","website":"
I’ve told you that it’s 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:
$arr = array(
'name' => 'Chen Yixin',
'nick' => 'Deep Space',
'contact' => array(
'email' => 'shenkong at qq dot com',
'website' => 'http://www.BkJia.com',
)
);
$json_string = json_encode($arr);
$obj = json_decode($json_string);
print_r($obj);
?>
Can I access the properties within the 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);
In the above, the json_encode string is wrapped in parentheses. After executing eval, it becomes a Javascript array (the technical term should not be called an array, but due to PHP's habits, I just call it an array for convenience. understand). 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 there is one point I will mention 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
The adjusted file profile.php
$arr = array(
'name' => 'Chen Yixin',
'nick' => 'Deep Space',
'contact' => array(
'email' => 'shenkong at qq dot com',
'website' => 'http://www.BkJia.com',
)
);
$json_string = json_encode($arr);
echo "getProfile('($json_string)')";
?>
Obviously, when index.html calls profile.php, a JSON string is generated and passed into getProfile as a parameter, and then the nickname is inserted into the div. In this way, a cross-domain data interaction is completed. Is it particularly simple for PHP to use JSON? .
Excerpted from chaojie2009’s column
http://www.bkjia.com/PHPjc/478372.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478372.htmlTechArticleYou may no longer be unfamiliar with PHP, but what about your understanding of JSON? Today we will tell you Briefly introduce the specific encoding of JSON used by PHP. I hope the content of this article will be useful to those who need it...