Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to master PHP JSON application correctly

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I would like to talk to you about how to correctly master the PHP JSON application, many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

PHP language has become a very popular application object in today's Internet world. Many programmers have begun to use it as the basic language for development. However, for AJAX, the resolution of XML is more inclined to the support of the foreground Javascript.

I think all the people who have parsed XML will have big heads because of trees and nodes. There is no denying that XML is a good way to store data, but its flexibility makes it difficult to parse. Of course, the difficulty referred to here is relative to the protagonist of this article, the PHP JSON app.

What is JSON? I won't repeat the concept. In popular terms, it is a format for storing data, just like a string serialized by PHP. It is a kind of data description. For example, if we serialize an array and store it, it can be easily deserialized and applied. The same is true of JSON, except that he builds an interactive bridge between the client-side Javascript and the server-side PHP. We use PHP to generate the string after JSON, and then pass this string to the foreground Javascript,Javascirpt, we can easily reverse JSON and apply it. To put it more colloquially, it's really like an array.

Back to the point, how to correctly master the application of PHP JSON. PHP5.2 has built-in support for JSON. Of course, if it is lower than this version, then there are many implementations of the PHP version on the market, and the next one will be OK. Now I'm mainly talking about JSON that PHP supports built-in. Quite simply, there are two functions: json_encode and json_decode (much like serialization). One encode, one decode. Let's first look at the use of coding:

$arr = array (

'name' = >' Chen Yixin'

'nick' = >' Deep Space'

'contact' = > array (

'email' = >' shenkong at qq dot com'

'website' = >

'http://www.chenyixin.com',

)

);

$json_string = json_encode ($arr)

Echo $json_string

? >

It is very simple to JSON an array. It should be pointed out that under non-UTF-8 encoding, Chinese characters cannot be encode, and the result will be null. Therefore, if you use gb2312 to write PHP code, you need to convert the content containing Chinese into UTF-8 using iconv or mb before json_encode. The output is as follows:

{"name": "\ u9648\ u6bc5\ u946b"

, "nick": "\ u6df1\ u7a7a", "

Contact ": {" email ":" shenkong

At qq dot com "," website ":

"http:\ / www.chenyixin.com"}}

I told you it's a lot like serialization, and you still don't believe it. After encoding, you need to decode it. PHP provides the corresponding function. After json_decode,json_decode is executed, you will get an object, as follows:

$arr = array (

'name' = >' Chen Yixin'

'nick' = >' Deep Space'

'contact' = > array (

'email' = >' shenkong

At qq dot com'

'website' = >

'http://www.chenyixin.com',

)

);

$json_string =

Json_encode ($arr)

$obj = json_decode ($json_string)

Print_r ($obj)

? >

Can you access the properties in the object? $obj- > name, in this way, of course, you can also transpose it into an array to make it easy to call:

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

PHP is not very useful for turning around. In addition to cache generation, it feels better to store arrays directly. However, when you interact with the foreground, its function comes out. Let's take a look at how I use this character with Javascript:

In the above, assign this string directly to a variable and it becomes an Javascript array (the professional term should not be an array, but because of the habit of PHP, I keep calling it an array, which is easy to understand). In this way, you can easily traverse the arr or do whatever you want. At this point, there seems to be no mention of AJAX? Yes, imagine that if the responseText returned by the server uses the JSON string instead of XML, isn't it convenient for the foreground Javascript to deal with it? That's how dog skin ointment is used.

In fact, at this point, except for the different storage formats of the data, there is not much difference between JSON and XML, but here's what I'm going to say. Although it doesn't have much to do with XML, it shows that PHP JSON applications have a wider range of applications, that is, cross-domain data calls. Due to security problems, AJAX does not support cross-domain calls, so it is troublesome to call data under different domain names, although there is a solution (stone mentioned proxies in his lecture, but he knows it can be solved). I wrote two files, enough to show cross-domain calls.

The main tone file index.html

Transferred file profile.php

$arr = array (

'name' = >' Chen Yixin'

'nick' = >' Deep Space'

'contact' = > array (

'email' = >

'shenkong at qq dot com'

'website' = >

'http://www.chenyixin.com',

)

);

$json_string = json_encode ($arr)

Echo "getProfile ($json_string)"

? >

Obviously, when index.html calls profile.php, the JSON string is generated, passed in to getProfile as a parameter, and then the nickname is inserted into the div, so that a cross-domain data exchange is completed. Since PHP JSON applications are so easy to use and easy to use, what are you waiting for?

After reading the above, do you have any further understanding of how to correctly master the PHP JSON application? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report