Before I wrote a lot of PHP web development in the application of XML for data interaction, the two days through the PHP parsing JSON and interactive examples to learn and understand the application of JSON in PHP, feel very convenient, in PHP parsing json mainly used Json_encode and JSON _decode two PHP JSON functions, much more convenient than PHP parsing XML, the following is a detailed introduction to the use of PHP json.
JSON Basics Introduction
JSON (JavaScript Object notation) is a lightweight data interchange format.
There are two main structures of JSON:
A collection of name/value pairs, which can be understood in PHP as associative arrays (associative array).
The ordered list of values (an ordered list of values). It can be interpreted as a normal array (array) in PHP.
Object is an unordered set of ' name/value pairs '. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" is followed by a ":" (a colon), and the ' name/value ' pair is separated by a ', ' (comma).
An array is an ordered collection of values (value). An array begins with "[" (left bracket), and "]" (right bracket) ends. Values are separated by the "," (comma) value. Values (value) can be enclosed in double quotes (string), numeric (number), True, False, NULL, objects (object), or arrays (array). These structures can be nested.
My current understanding of JSON is more of an array, similar to an associative array in PHP, where you can convert the PHP array to JSON format.
For more information on JSON please refer to the JSON website and IBM's introduction to JSON
PHP JSON Parse Instance
PHP5.2 began to have JSON as part of the PHP extension, so you don't need to install JSON separately. In addition, in order to ensure that the following PHP JSON instances do not appear in Chinese garbled, please ensure that your coding format is UTF8.
JSON is part of JavaScript, first we look at how to define JSON in JavaScript
1 2 3 4 5 6 7 8
|
var json_obj = {' WebName ': ' PHP Web Development Tutorial Network '}; Alert (json_obj. WebName);
var json_arr =[{' WebName ': ' PHP Web Development tutorial Web ', ' WebSite ': ' http://www.leapsoul.cn '},{' arttitle ': ' PHP json instance '} ' Alert (json_arr[1). Arttitle);
var Php_json = [[' PHP Website Development tutorial Net ', ' http://www.leapsoul.cn '],[' article title ', ' PHP json instance ']]; Alert (php_json[1][0]); |
In this JSON instance code I built all the JSON forms,
Line 1th, which defines the existence of JSON as an object
Line 4th, which defines JSON as an array and nested JSON objects, is somewhat analogous to an associative array of PHP, but is still an object.
Line 7th, the JSON is set to exist as a normal array.
Note : Because JavaScript is case-sensitive, you should be aware of the capitalization of the key when you access the JSON associative array object.
When PHP interacts with JSON-formatted data, the first step is to convert the PHP array into JSON-formatted data, using the PHP5 Json_encode function, which is used when PHP parses the JSON-formatted data passed Json_ The decode function is parsed into a PHP array, and the instance code that PHP transforms and parses the JSON data is as follows
1 2 3 4 5 6 7 8 9 10 11
|
<?php $json _arr = Array (' WebName ' => ' PHP Web Development Tutorial Network ', ' WebSite ' => ' http://www.leapsoul.cn ');
$php _json = Json_encode ($json _arr);
echo $php _json;
$php _json = Json_decode ($php _json);
Print_r ($php _json); ?> |
Description : This defines the associative array through PHP, and then converts it to JSON-formatted data via Json_encode, and the Echo statement lets you see the converted JSON object and parse the function via PHP json json_ Decode can parse JSON-formatted data into an associative array of PHP. If the encoding is not correct, JSON will appear garbled in Chinese. If you use EditPlus to debug PHP, note that the encoding changes ANSI to UTF8 when you save the file.
Finally, let's look at the complete PHP JSON interaction instance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23
|
<?php $json _arr = Array (' WebName ' => ' PHP Web Development Tutorial Network ', ' WebSite ' => ' http://www.leapsoul.cn ');
$php _json = Json_encode ($json _arr); ?>
<script type= "Text/javascript" >
var Php_json = <?= $php _json?>;
</script>
<script type= "Text/javascript" >
function Php_json_dis (Php_json) { Alert (Php_json. WebName); Alert (Php_json. WebSite); }
Php_json_dis (Php_json);
</script> |
At this point, PHP through the Json_encode and Json_decode functions of the JSON format data conversion and resolution of the example is finished.
Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.