It is based on a subset of JavaScript programming Language, Standard ECMA-262 3rd edition-december 1999
JSON mainly uses the pairs of {} to wrap each object (object), in pairs [] to package each array (array),
Wrap the strings with a pair of "", and use the symbols to separate the variables, and the data type has string, number, array, object
The following simple JSON format, Syria, describes an object json that has 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 features JSON decoder & Encoder '},
{' id ': ' 2 ', ' type ': ' B ', ' title ': ' JSON is all JavaScript Object notation '},
{' array ': [' A ', ' B ', ' C ', ' D ', ' E ']}
]
};
So, we can get an object called JSON, and this JSON object contains a unique member query
And query contains an array with three object in it, and the first two object contains three members
Id,type,title, and the last object array contains an array, so is the explanation clear?
But how to use it?
It's 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 is all called JavaScript Object notation
Alert (' array index 3= ' +json.query[2].array[3]);
Alert array index 3=d
It's easier to manipulate data, 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 I can get the value of the title contained in the first pen.
The development of PHP is so fast that when the programmer knows nothing about JSON or when it's completely out of JSON,
PHP is already in the core of the latest version 5.2.0, and the preset is enabled, compared to other script languages
PHP can be called first, in the 5.2.0 version of the JSON is actually two functions Json_decode () and Json_encode ()
The former is a JSON-formatted string that is originally a php-native array
The latter is to translate the PHP native array into a JSON-formatted string
However, since JavaScript supports Unicode, if you use non-ASCII characters when accessing a data library, such as China, Japan, and Korea
You need to convert the character code to UTF8, or the string after Json_encode () will be the code.
========================================================
After a simple introduction to JOSN,
This article is actually about how to use Josn
The following example uses a version that requires more than MySQL4.1
Code full use UTF8
To undertake the previous data format, the table has a total of three column Id,type,title
Data table specifications are as follows
Copy content to Clipboard code:
CREATE TABLE ' News ' (
' ID ' int (a) 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:
<?php
Establish a connection
$conn = Mysqli_connect ("localhost", ' root ', ') or Die (' not connected to the repository ');
Select a library
mysqli_select_db ($conn, ' mydata ') or Die (' cannot select Library ');
To set the wiring code, you don't know how to find Google.
Mysqli_query ($conn, ' SET NAMES ' utf8 ');
Remove data
$results = Mysqli_query ($conn, ' SELECT id,type,title from News ');
Josn string
$json = ';
Because it is a case, it controls the loop
$i = 0;
while ($row = Mysqli_fetch_assoc ($results))
{
$i + +;
$json. = Json_encode ($row);
There are only three materials in the data table, so you don't need to add "," to the tail in the third pen, remember, the last piece of information does not have to add ","
if ($i <3)
{
$json. = ",";
}
}
To package the data into a column
$json = ' {' query ': ['. $json. ']} ';? >
<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">
<title>json Case </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta http-equiv= "Pragma" content= "No-cache"/>
<meta http-equiv= "Expires" content= "-1"/>
<meta http-equiv= "Cache-control" content= "No-cache"/>
<meta name= "generator" content= "Mamba"/>
<body>
<script type= "Text/javascript" >
var json = <?php echo $json?>;
Alert (' I have ' +json.query.length + ' object. ');
Alert (' type= ' +json.query[1].type+ ' rntitle ' +json.query[1].title);
Used in the last simple article
</script>
Still original json<br>
<?php
To decipher a string
$s _json_decoded = Json_decode ($json, true);
Retrieve information
foreach ($s _json_decoded as $row)
{
foreach ($row as $rowa)
{
echo $rowa [' title ']. <br> ";
}
}
?>
</body>
After a simple training
I'm sure everyone has a deeper understanding of the JSON thing.
Of course, the application of JSON is not just that simple example
Interested in studying together.
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.