PHP6 First Class JSON instance code _php tutorial

Source: Internet
Author: User
It is based on a subset of JavaScript programming Language, Standard ECMA-262 3rd Edition-december 1999
JSON mainly uses the paired {} to wrap each object (object), using the paired [] to wrap each array,
Use the paired "" to wrap the strings, use the numbers to separate the variables and the information type is string, number, array, object

The following is a simple JSON format, which describes an object JSON with a member variable, which contains three objects.

Copy content to Clipboard code:
var json = {
' Query ': [
{' id ': ' 1 ', ' type ': ' A ', ' title ': ' PHP 5.2.0 ' new feature JSON Decoder & Encoder '},
{' id ': ' 2 ', ' type ': ' B ', ' title ': ' JSON-All-JavaScript ' Object Notation '},
{' array ': [' A ', ' B ', ' C ', ' D ', ' E ']}
]
};
So, we can get an object called JSON, and this JSON object contains a stand-alone member query
The query contains an array with three objects in the array and two objects in the front containing three members
Id,type,title, and the last object array contains an array, so you understand?

But how to use it?
It's pretty simple.
Alert (' I have ' +json.query.length + ' object. ');
Alert I has 3 object.
Alert (' type= ' +json.query[1].type+ ' \r\ntitle ' +json.query[1].title);
Alert Type=b Title=json Full-call JavaScript Object Notation
Alert (' array index 3= ' +json.query[2].array[3]);
Alert array index 3=d

This way, it's easier to do things without having to deal with complex DOM, and the information you need is easy to get.
For example, the above example json.query[I].title so that I can get the title of the first pen contains the value
PHP's development is very fast, when the programming world is smattering or completely unaware of JSON
PHP has been 5.2.0 to the core in the latest version of the release, and the preset state is enabled, compared to other script languages
PHP is called a horse first, in version 5.2.0 The JSON actually has two functions Json_decode () and Json_encode ()
The former is an array of JSON-formatted strings that are native to PHP
The latter is to encode the PHP native array into a JSON-formatted string
But, because JavaScript supports Unicode, if you use non-ASCII characters when accessing the repository, such as Chinese, Japanese, and Korean
You need to convert the character encoding to UTF8, or the string after Json_encode () will be a code
========================================================
After a simple introduction to JOSN,
This article is about how to use Josn
The following example uses the MySQL4.1 version above
The full use of UTF8
To undertake the previous information format, the table has a total of three linked fields bit Id,type,title
The information table is as follows
Copy content to Clipboard code:
CREATE TABLE ' News ' (
' id ' int (ten) unsigned not NULL auto_increment,
' type ' varchar (255) Not NULL default ' ',
' title ' varchar (+) not NULL default ' ',
PRIMARY KEY (' id ')
) Engine=myisam auto_increment=0 DEFAULT Charset=utf8;
Copy content to Clipboard code:
Connect
$conn = Mysqli_connect ("localhost", ' root ', ') or Die (' not connected to the repository ');
Select the Repository
mysqli_select_db ($conn, ' mydata ') or Die (' cannot select the repository ');
Set the connection code rules, do not understand Google to find
Mysqli_query ($conn, ' SET NAMES ' utf8 ');
Remove the information
$results = Mysqli_query ($conn, ' SELECT id,type,title from News ');
Josn string
$json = ";
Because this is an example, self-control loops
$i = 0;
while ($row = Mysqli_fetch_assoc ($results))
{
$i + +;
$json. = Json_encode ($row);
There are only three notes in the information table, so you do not need to add "," at the end of the third pen, remember, the last piece of information does not add ","
if ($i <3)
{
$json. = ",";
}

}
To pack the information into a column
$json = ' {' query ': ['. $json. ']} ';? >



<title>JSON example</title>








Still original JSON

Encode the string
$s _json_decoded = Json_decode ($json, true);
Retrieving information
foreach ($s _json_decoded as $row)
{
foreach ($row as $rowa)
{
echo $rowa [' title '].
";
}

}
?>


After a simple practice,
I believe we have a deeper understanding of the JSON thing.
Of course, JSON is not just a simple example.
Be interested in studying together.

http://www.bkjia.com/PHPjc/319271.html www.bkjia.com true http://www.bkjia.com/PHPjc/319271.html techarticle It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999 JSON mainly uses the paired {} to wrap each object ), in pairs ...

  • 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.