In PHP, you can use json_decode ($ str) to convert the string back. Why is it a JSON object? Does PHP have default resolution? Direct output is like this. Then the json_decode (, true) is like this.
In PHP, you can use json_decode ($ str) to convert the string back.
Why is it a JSON object? Does PHP have default resolution?
Direct output is like this.
Then the json_decode (, true) is like this.
Reply content:
In PHP, you can use json_decode ($ str) to convert the string back.
Why is it a JSON object? Does PHP have default resolution?
Direct output is like this.
Then the json_decode (, true) is like this.
First, you need to understand what JSON is,JavaScript Object NotationIt refers to the JavaScript Object Notation, which is a lightweight data exchange format based on text and independent of languages. To put it bluntly, it is a string. Because no additional tag is attached, JS can also be directly processed as an object, so it is often used for data exchange.
PHP recognizes the JSON string because the PHP library containsJSON_parser
To resolve
This is like an exchange between two tribes, even though the language is not available. This 'item' is ours.Data
Since javascript can directly process JSON objects, why do the JSON strings obtained by AJAX requests need to be converted to JSON objects?JSON stringAndJSON objectNow
JSON string: a js string that meets the JSON format requirements. As follows:
var jsonStr = "{id:'1',name:'Fini',age:30}";
JSON object: a js object that meets the JSON format requirements. As follows:
var jsonObj = { id: "1", name: "Fini", age: 30 };
In PHP, the related operations are as follows:json_encode
,json_decode
json_encode
Theoretically, all data types except resourse can be convertedJSON string(But sometimes it will fail ~ Token ~)
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);var_dump(json_encode($arr));/**** result by php5.6 ****/string '{"a":1,"b":2,"c":3,"d":4,"e":5}' (length=31)
json_decode
IsJSON stringConvert to PHParray
Orobject
, Depending on the second parameter (true
/false
)
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';var_dump(json_decode($json));var_dump(json_decode($json, true));/**** result by php5.6 ****/object(stdClass)[1] public 'a' => int 1 public 'b' => int 2 public 'c' => int 3 public 'd' => int 4 public 'e' => int 5array (size=5) 'a' => int 1 'b' => int 2 'c' => int 3 'd' => int 4 'e' => int 5
By default, json_decode is converted to an object, and the array is added with ture.
Instructions on json_decode in the PHP Manual
Mixed json_decode (string $ json [, bool $ assoc])
Accept oneJSON stringAnd convert it to a PHP variable.