PHP Tutorial-php Tutorial

Source: Internet
Author: User
The code of the PHP6 prefix JSON instance. It is based on JavaScriptProgrammingLanguage, a subset of the StandardECMA-2623rdEdition-December1999 JSON mainly uses the correct {} to wrap each object (object), it is based on JavaScript Programming Language, A subset of Standard ECMA-262 3rd Edition-December 1999.
JSON mainly uses the correct {} to wrap each object (object), and uses the correct [] to package each array (array column ),
Use a pair of "" to wrap each string, and use a pair to block different data types. The data types include string, number, array, and object.

The following is a simple JSON format, which describes the number of members in an object json file. This number includes three objects.

Copy the content to the clipboard code:
Var json = {
'Query ':[
{'Id': '1', 'type': 'A', 'Title': 'json decoder & encoder '},
{'Id': '2', 'type': 'B', 'Title': 'json fully formatted JavaScript Object notation '},
{'Array': ['A', 'B', 'C', 'D', 'E']}
]
};
In this way, we can retrieve an Object called json, and this json Object contains an independent member query
Query contains an Array, which contains three more objects. The first two objects contain three members.
Id, type, title, and the last Object array contains a response column. do you still understand this solution?

But how can we use it?
Very simple
Alert ('I have' + json. query. length + 'object .');
// Alert I have 3 object.
Alert ('Type = '+ json. query [1]. type +' \ r \ ntitle '+ json. query [1]. title );
// Alert type = B title = JSON fully formatted JavaScript Object Notation
Alert ('expect column index 3 = '+ json. query [2]. array [3]);
// Alert primary column index 3 = D

In this way, the operation is more convenient, and you do not need to deal with the complex DOM. the required information can be obtained quickly.
For example, in the preceding example, json. query [I]. title can obtain the value in the title of the I partition.
The development of PHP is very rapid. when the program community still knows about JSON, or does not know why JSON
PHP has been written into the core in the latest version 5.2.0, and is used for programming. similar to other Script scripts
PHP can be used first. in version 5.2.0, two functions json_decode () and json_encode () are provided for JSON ()
The former is to restore JSON-format strings to PHP native character columns.
The latter is to merge the native PHP response column into a JSON string.
However, Javascript supports Unicode. if non-Ascii characters are used when accessing the resource, such
You need to convert the character into UTF8; otherwise, the string after json_encode () will be invalid.
========================================================== ======================
After the previous ticket introduced JOSN
This article describes how to use JOSN.
The following example uses MySQL4.1 or a later version.
During the entire process, use utf8
Take the data format of the previous article. The table has three parameter IDs, type, and title.
The specifications of the data table are as follows:
Copy the content to the clipboard code:
Create table 'news '(
'Id' int (10) unsigned not null auto_increment,
'Type' varchar (255) not null default '',
'Title' varchar (64) not null default '',
Primary key ('id ')
) ENGINE = MyISAM AUTO_INCREMENT = 0 default charset = utf8;
Copy the content to the clipboard code:
// Create a zookeeper
$ Conn = mysqli_connect ("localhost", 'root', '') or die ('unable to upload data output ');
// Select information
Mysqli_select_db ($ conn, 'mydata') or die ('No resource select ');
// Set the specification rules. if you are not familiar with google
Mysqli_query ($ conn, 'set names' utf8 '');
// Retrieve Information
$ Results = mysqli_query ($ conn, 'Select id, type, title FROM news ');
// Josn string
$ Json = '';
// Because it is an example, you can control the incircle
$ I = 0;
While ($ row = mysqli_fetch_assoc ($ results ))
{
$ I ++;
$ Json. = json_encode ($ row );
// The data table only contains three types of data. Therefore, you do not need to add "," to the tail during the third period. Remember, you do not need to add ","
If ($ I <3)
{
$ Json. = ",";
}

}
// Import the data package into the inventory column
$ Json = '{"query": ['. $ json. ']}';?>



Json example







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.