In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "application skills of JSON in PHP". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn the "application skills of JSON in PHP" together.
Today, AJAX is no longer a strange word. When you think of AJAX, you may immediately think of XML, which was born of RSS. Admittedly, XML is a good way to store data, but its flexibility just 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 PHP strings after serialization. It's 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, but it bridges the interface between client-side Javascript and server-side PHP. We use PHP to generate the string after JSON, and then pass this string to the foreground Javascript, Javascript can easily reverse it JSON and then apply it. In layman's terms, it's really like arrays.
How to use JSON. PHP 5.2 starts with built-in JSON support. Of course, if it is lower than this version, then there are many PHP versions on the market, and the next one is OK. Now let's talk about PHP's built-in support for JSON. It's simple, two functions: json_encode and json_decode (much like serialization). One code, one decode. Let's look at the use of coding:
It's easy to create an array of JSON. It should be pointed out that under non-UTF-8 encoding, Chinese characters will not 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 contents containing Chinese to UTF-8 and then json_encode. The output above is 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 looked like serialization, and you didn't believe me. PHP provides the corresponding function json_decode. After json_decode is executed, an object will be obtained. The operation is as follows:
Access properties within objects, right?$ obj->name, like this, of course, can also be inverted array, convenient call it:
$json_string = json_encode($arr); $obj = json_decode($json_string); $arr = array($obj); print_r($arr);
PHP to turn around the use is not particularly large, in addition to cache generation, it feels better than directly save the array, but when you interact with the foreground, its role comes out, let's see how I use Javascript to use this character:
var arr = {"name":"\u9648\u6bc5\u946b","nick":"\u6df1\u7a7a", "contact":{"email":"shenkong at qq dot com","website":"http:\/\/www.chinaz.com"}}; alert(arr.name)
In the above, directly assign this string to a variable, and it becomes a Javascript array (the technical term should not be called an array, but due to PHP habits, I will always call it an array, which is easy to understand). This way, you can easily traverse arr or do whatever you want. I don't think there's any mention of AJAX here. Yes, associate it, if the responseText returned by the server uses a JSON string instead of XML, is it very convenient for Javascript to process? Dog skin plaster is used in this way.
In fact, written here, in addition to the data storage format is not the same, JSON and XML is not much different oh, but the following I said a point. Although it has little to do with XML, it illustrates the broader application of JSON, which is cross-domain data invocation. Due to security issues, AJAX does not support cross-domain calls, so to call data under different domain names, very troublesome oh, although there are solutions (stone in his lecture mentioned agent ah what although do not understand but know can solve). I wrote two files that were enough to demonstrate the cross-domain invocation.
Main theme file index.html
function getProfile(str) { var arr = str; document.getElementById('nick')[xss_clean] = arr.nick; }
The requested file is profile.php
Obviously, when index.html calls profile.php, a JSON string is generated and passed as a parameter to getProfile, and then the nickname is inserted into the div, so that a cross-domain data interaction is completed. Is it particularly simple?
Thank you for reading, the above is the content of "JSON application skills in PHP", after the study of this article, I believe that everyone has a deeper understanding of the application skills of JSON in PHP, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.