Look at a JS function first
Copy CodeThe code is as follows:
function Jsontest ()
{
var json = [{' username ': ' crystal ', ' userage ': '},{' username ': ' Candy ', ' userage ': ' 24 '}];
alert (json[1].username);
var json2 = [' Crystal ', '],[' Candy ', ' 24 '];
Alert (json2[0][0]);
}
This function, the first alert (json[1].username); "Candy" will be prompted. A JSON variable is an array object. So it's going to be called in a format like obj.username.
A second alert (json2[0][0]); Will prompt "Crystal". The Json2 variable is completely in a JSON format. Both JSON and JSON2 variables achieve the same effect, but json2 is significantly leaner than JSON.
This is the JSON format for JavaScript.
Let's look at the JSON format in PHP.
Let's look at some code first.
Copy CodeThe code is as follows:
$arr = Array (
Array (
' catid ' = ' 4 ',
' CatName ' = ' regulation regulation ',
' Meta_title ' = ' Regulation Regulation Blog '
),
Array (
' catid ' = ' 6 ',
' CatName ' = ' climber ',
' Meta_title ' = ' The Climber ',
)
);
$jsonstr = Json_encode ($arr);
Echo $jsonstr;
In this code, $arr is an array, and we use Json_encode to convert $arr to JSON format.
This code will output:
[{"CatID": "4", "CatName": "\u7a0b\u7a0b", "Meta_title": "\u7a0b\u7a0b\u535a\u5ba2"},{"catid": "6", "CatName": " Climber "," Meta_title ":" \u6500\u767b\u8005 "}]
This is what PHP does with JSON data.
For JSON data, PHP can also use the Json_decode () function to convert JSON data to an array.
For example, in the above code, we use the Json_decode function to deal with. The above array is printed again.
$jsonstr = Json_encode ($arr);
$jsonstr = Json_decode ($JSONSTR);
Print_r ($JSONSTR);
Next, let's see how the PHP JSON data and the JS JSON data call each other.
We create a new php_json.php file
The code is as follows:
Copy CodeThe code is as follows:
$arr = Array (
Array (
' catid ' = ' 4 ',
' CatName ' = ' regulation regulation ',
' Meta_title ' = ' Regulation Regulation Blog '
),
Array (
' catid ' = ' 6 ',
' CatName ' = ' climber ',
' Meta_title ' = ' The Climber ',
)
);
$jsonstr = Json_encode ($arr);
-----below the PHP range-----
var jsonstr=<? = $jsonstr? >;
PS: Var jsonstr=< at the end of php_json.php file? = $jsonstr? >; This sentence. This is to assign the data in JSON format to the JSONSTR variable.
We'll build a json.html file.
The code is as follows:
Copy CodeThe code is as follows:
This way, when we look at json.html, Loadjson (JSONSTR) will prompt "Regulation regulation" and "climber"
This also implements the JS cross-domain invocation.
http://www.bkjia.com/PHPjc/326029.html www.bkjia.com true http://www.bkjia.com/PHPjc/326029.html techarticle first look at a JS function copy code code as follows: function jsontest () {var json = [{' username ': ' crystal ', ' userage ': '},{' username ': ' Candy ', ' Userage ': ' 24 '}]; Alert (json[1].username) ...