Correctly Master PHP JSON Application_PHP Tutorial
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>
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>
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>
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>
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>
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?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
