Processing of js returned data by php json_decode
After php json_decode, the data returned to the front-end is: encode_str = "{" green ": 10," size ": 5," strock ": 12}
Then js uses eval ("obj =" + encode_str + ";");
Json data can be instantiated as an object, and data can be obtained directly by obj. green.
In Javascript, {} can be used to represent an object, and [] can be used to represent an array, for example:
Var obj = {"a": "v", "B": "x"}; // This indicates that the variable obj is an object and has two attributes: a and B, the attribute values are v and x.
Var arr = ["v", "x"]; // This indicates that the arr variable is an array with two elements. The indexes are 0 and 1, and the values are: v and x.
JSON is the format of the logical structure of the data in which the two formats are mixed. In fact, JSON is a mixture of objects and arrays in Javascript.
PHP provides specialized functions to generate and parse JSON-format data. The data parsed by PHP has the same meaning as the original Javascript data, that is, the Javascript Object is parsed into a PHP Object, the Javascript array is parsed into a PHP array. The JSON function used by 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.
Copy codeThe Code is as follows: // Form 1: completely in the form of an object, this form of data in Javascript
Is also called the related array, which is different from the general array,
It can be accessed through string indexing (using "[]" or "."
To indicate the level)
$ Json = '{"item1": {"item11": {"n": "chenling ",
"M": "llll"}, "sex": "male", "age": "25"}, "item2 ":
{"Item21": "ling", "sex": "female", "age": "24 "}}';
$ J = json_decode ($ json );
Print_r ($ J );
Output:Copy codeThe Code is as follows: stdClass Object
(
[Item1] => stdClass Object
(
[Item11] => stdClass Object
(
[N] => chenling
[M] => llll
)
[Sex] => male
[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.Copy codeThe Code is as follows: // Form 2: object and array are mixed
$ 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, the JSON format of this PHP application combines the access methods of objects and arrays, which is also equivalent to accessing a 5-dimensional array.Copy codeThe Code is as follows: // Form 3: Full array form
$ 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, it is equivalent to accessing a 2-dimensional array.
JSON Summary of PHP applications:
From the JSON example of the PHP application, we can see that JSON is a bit similar to XML. It can also transfer structured data between PHP and Javascript, which is very convenient to use.
Note that each attribute and attribute value are enclosed by quotation marks.