PHPJSON mutual function performance introduction. The conversion from json_encode data to JSONjson_decodeJSON to PHP variables is particularly important when using Ajax functions such as jquery and Ext frameworks. the default number of values accepted by Ext
Json_encode data to JSON
Conversion from json_decode JSON to PHP variables
These two php json conversion functions are particularly important when using Ajax such as jquery and Ext frameworks. Ext accepts JSON data by default, jquery makes JSON access easier and more concise. This article will use an example to discuss the application of php json data functions in jquery.
Jquery, as a lightweight Ajax framework, is indeed short and refined. it is more suitable for front-end pages of Web programming and is more suitable for integration with PHP. Among them, jquery has a method: $. getJSON, which is used to obtain data in JSON format from the server script page. We first create a php script named json. PHP in order. the specific program is as follows:
- <? Php
- // Create a Union array. here we try to make it true
Simulate a record read from the database
- $ A = array ('name' => 'dog', 'sex' =>
'F', 'par' => array ('Lil', 'Wang ', 'liany '));
- $ B = array ('name' => 'pig', 'sex' =>
'M', 'par' => array ('liv', 'wange ', 'Liang '));
- $ Arrayarray = array (0 => $ a, 1 => $ B );
- // Convert the array into a json data string
- Echo json_encode ($ array );
- ?>
Jquery code on the front-end page:
- <Script>
- $ (Document). ready (function (){
- $. GetJSON (
- 'Json. php ',
- Function (data ){
- Var content = "";
- For (var I = 0; I
{// Cyclically read the JSON data obtained from the background
- Content + = data [I]. name;
- Var par = "blocking :";
- For (var j = 0; j
Length; j ++) {// read the par set in the data loop
- Par + = data [I]. par [j] + ",";
- }
- Content + = par +"
";
- }
- Alert (content );
- }
- );
- });
- </Script>
In this example, the basic process control for js is applied to read data cyclically. in jquery, a traversal method is also provided. if you are interested in each, you can understand this method by yourself, the underlying implementation is also based on the for loop.
Note: The json_encode json_decode two php json conversion functions are available only after PHP5.2. if you use them earlier than the previous version, an error indicating that the function cannot be found will be reported!
Json_decode JSON to PHP variable conversion these two php json conversion functions are particularly important when using Ajax such as: jquery and Ext frameworks, the default number accepted by Ext...