We have to use Content-type:text/json to display the JSON data properly.
The code is as follows |
Copy Code |
<?php
Header (' Content-type:text/json '); This is the point, it tells the object receiving the data this page output is JSON data;
$json ={"n": "Name", "P": "Password"}; Although this line of data is in the form of JSON format, it is not processed as JSON data without the above sentence;
Echo $json;
?> |
Json_encode Of course you can also enter the JSON string, see below for a few examples.
The function that PHP generates JSON is: Json_encode ($PHPcode);
The function of PHP parsing JSON is: Json_decode ($JSONcode);
So there are many forms of JSON, and different forms are different in the form of PHP interpretation.
//Form 1: A completely object form in which the data is called an associated array in JavaScript, unlike a generic array, it can be accessed using a string as an index (with "[]" or "." To represent the hierarchy)
code is as follows |
copy code |
$json = ' {' Item1 ': {"Item11": {"n": "Chenling", "M": "Llll"}, "Sex": "Www.111cn.net", "Age": "A"}, "item2": {"Item21": "Ling", "Sex": "Female", "Age": "24"}} '; $J =json_decode ($json); Print_r ($J); Output: StdClass Object ( [item1] => stdClass Object ( [ITEM11] => StdClass Object ( [n] => chenling [m] => llll ) [sex] => www.111cn.net [age] => 2 5 ) [item2] => stdClass Object ( [item21] => ling [sex] => women [age] => ; ) |
For example, if I'm going to get the attribute that the value is chenling, you should access it like this:
$J->item1->item11->n;//This gets the value of the property N: chenling
In fact, this type of access is similar to accessing normal object properties, and it is equivalent to accessing a 3-D array.
Form 2: Object and array blending
code is as follows |
copy code |
$json = ' {"item1": [{"Name": [{"Chen": "Chenling", "Ling": "Chenli"}], "Sex": "Male" , "Age": "},{" "Name": "Sun", "Sex": "Female", "Age": "24"}]} '; $J =json_decode ($json); Print_r ($J); Output: StdClass Object ( [item1] => Array ( [0] => StdClass object ( [ Name] => Array ( [0] => stdClass Object ( [Chen] => chenling [Ling] => Chenli ) ) [sex] => man [age] => ) [ 1] => stdClass Object ( [name] => Sun [sex] => female [age] => ) ) |
For example, if I'm going to get the element that the value is chenling, you should access it like this:
$J->item1[0]->name[0]->chen;//This will get the element Chen's value: chenling
In fact, this form of access combines the access of objects and arrays, as well as accessing a 5-D array.
Form 3: Complete array form
The code is as follows |
Copy Code |
$json = ' [[item1 ', ' item11 '],[' n ', ' chenling '],[' m ', ' llll ']] '; $J =json_decode ($json); Print_r ($J); Will output: Array ( [0] => Array ( [0] => item1 [1] => ITEM11 ) [1] => Array ( [0] => N [1] => chenling ) [2] => Array ( [0] => m [1] => llll ) ) |
For example, if I'm going to get the element that the value is chenling, you should access it like this:
$J [0][1];//This will get the element value chenling the element
But there is a disadvantage in this way is that the string can not be indexed, only with numbers, in the form of a complete object to solve this problem
In fact, this form of access is the way the array is accessed, equivalent to accessing a 2-D array.
Summary:
As you can see from the example above, JSON is a bit like XML, and it can be used to transfer data with structure between PHP and JavaScript.