Php directly outputs the json format. many new users have a misunderstanding that echojson_encode ($ data) is used to output json data. If yes, the output text is in json format rather than json data, correct writing... php directly outputs the json format. many new users have a misunderstanding that echo json_encode ($ data) is used to output json data. If yes, the output text is in json format rather than json data, the correct syntax is to add a sentence. we must use Content-type: text/json to display json data normally.
The PHP instance code is as follows:
Json_encode can also be a json string. The following are several examples.
The JSON generation function of PHP is: json_encode ($ PHPcode );
The JSON parsing function of PHP is: json_decode ($ JSONcode );
Therefore, there are multiple JSON forms. different forms are different after PHP explains them.
Form 1: completely in the form of objects. this form of data is also called a related array in Javascript. What is different from a general array is that, it can be accessed through string indexing (using "[]" or ". the code is as follows:
$ Json = '{"item1": {"item11": {"n": "chenling", "m": "llll"}, "sex": "www.phprm.com ", "age": "25"}, "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.phprm.com [age] => 25) [item2] => stdClass Object ([item21] => ling [sex] => Female [age] => 24 ))*/
For example, if I want to obtain the property whose value is chenling, we should access it like this:
$ J-> item1-> item11-> n; // This will get the value of attribute n: chenling
In fact, this access form is similar to accessing common object attributes, and is also equivalent to accessing a 3-dimensional array.
Form 2: Object and array are mixed. the code is as follows:
$ Json = '{"item1": [{"name": [{"chen": "chenling", "ling": "chenli"}], "sex ": "male", "age": "25" },{ "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] => male [age] => 25) [1] => stdClass Object ([name] => sun [sex] => Female [age] => 24 )))*/
For example, if you want to obtain the element whose value is chenling, you should access it as follows:
$ J-> item1 [0]-> name [0]-> chen; // This will get the value of the element chen: chenling
In fact, this access method combines object and array access, which is equivalent to accessing a 5-dimensional array.
Form 3: Complete array format, the code is as follows:
$ Json = '[["item1", "item11"], ["n", "chenling"], ["m", "llll"]'; $ J = json_decode ($ json); print_r ($ J);/* output: array ([0] => Array ([0] => item1 [1] => item11) [1] => Array ([0] => n [1] => chenling) [2] => Array ([0] => m [1] => llll ))*/
For example, if you want to obtain the element whose value is chenling, you should access it as follows:
$ J [0] [1]; // the element whose element value is chenling.
However, there is a disadvantage in this method: strings cannot be used as indexes and only numbers can be used. this problem can be solved in the form of full objects. in fact, this access form is an array access method, which is equivalent to accessing a 2-dimensional array.
Summary: the preceding example shows that JSON is similar to XML, and structured data can be transferred between PHP and Javascript, which is convenient to use.
Article link:
Save this article for favorites!