PHP using the JSON technique

  • 2020-05-27 04:36:47
  • OfStack

php json_decode returns the processing of data js

After php json_decode, the data returned to the foreground is: encode_str = "{"green":10,"size":5,"strock":12}
Then js passes eval("obj = "+ encode_str + ";") );
You can instantiate the json data into an object, and directly obj.green can get the data.

In Javascript, you can use {} to represent 1 object and [] to represent 1 array, such as:

var obj = {" a ":" v ", "b" : "x"}; // this means that the variable obj is an object with two properties: a and b, and the property values are v and x, respectively.
var arr = [" v x ", ""]. // this means that the variable arr is an array with two 1 elements, indexes 0 and 1, and values v and x.
While JSON is a combination of the two formats in 1 to represent the logical structure of the data, JSON is a combination of objects and arrays in Javascript

PHP provides a special function to generate and parse JSON data. PHP parses the data root of Javascript data, i.e., Javascript objects are parsed into PHP objects, Javascript array into PHP array, PHP JSON functions are: json_encode($PHPcode);
The function PHP parses JSON is: json_decode($JSONcode);

Therefore, there are various forms of JSON, and different forms are also different after PHP is explained.


// In the form of 1 : completely in the form of an object , This form of data is in Javascript
 Is also called a correlation array, and 1 Unlike arrays, 
 It can be accessed by using a string as an index ( With" [] "Or" . " 
 To represent the hierarchy )   
$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); 

The output:

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 get the property whose value is chenling, I should access it like this:
$J- > item1- > item11- > n; // this will get the value of the attribute n: chenling
In fact, this access is similar to the access to ordinary object properties, also equivalent to access a three-dimensional array.

// In the form of 2 : object and array mix    
$json='{"item1":[{"name":[{"chen":
"chenling","ling":"chenli"}],"sex":
" male ","age":"25"},{"name":"sun","sex":
" female ","age":"24"}]}';   
$J=json_decode($json);   
print_r($J);   
 The 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 I want to get the element whose value is chenling, I should access it like this:
$J- > item1[0]- > name[0]- > chen; // this will get the value of the element chen: chenling
In fact, PHP USES the form JSON to access objects and arrays, which is equivalent to accessing a 5-dimensional array.

// In the form of 3 : full array format    
$json='[["item1","item11"],[
"n","chenling"],["m","llll"]]';   
$J=json_decode($json);   
print_r($J);   
 The output:    
Array   
(   
[0] => Array   
(   
[0] => item1   
[1] => item11   
)   
[1] => Array   
(   
[0] => n   
[1] => chenling   
)   
[2] => Array   
(   
[0] => m   
[1] => llll   
)   
) 

For example, if I want to get the element whose value is chenling, I should access it like this:

$J [0] [1]; // this will get the element whose value is chenling

However, there is a disadvantage in this way, that is, you can't use a string as an index, you can only use a number, and you can solve this problem in the form of a complete object. In fact, this access form is the access way of an array, equivalent to access a 2-dimensional array.

PHP application JSON summary:

From the above example of PHP applying JSON, it can be seen that JSON is somewhat similar to XML. It can also transfer structured data between PHP and Javascript, which is very convenient to use.
Note that each attribute and its value are enclosed in quotation marks "".


Related articles: