generate JSON data in PHP (Json_decode ())
The data format generated by the parameter for the indexed array is the JS array
$fruit = Array (' Apple ', ' banana ', ' pear ', ' orange '); $fruit _jn = Json_encode ($fruit); [' Apple ', ' banana ', ' pear ', ' orange ']
The data generated by the parameter for the associative array is in JSON object format
$fruit = Array (' a ' = = ' Apple ', ' b ' = ' banana ', ' c ' = ' pear ', ' d ' = ' orange '); $fruit _jn = Json_encode ($fruit); /{"A": "Apple", "B": "Banana", "C": "Pear", "D": "Orange"}
The data generated by the parameter is also a JSON object format that ignores the methods of the object
$animal = Array (' A ' =>array (' aa ' + = ' cat ', ' bb ' = ' pig '), ' C ' =>array ("cc" = ' chicken ', "dd" = ' duck ')); echo Json_encode ($animal); {"A": {"AA": "Cat", "BB": "Pig"}, "C": {"CC": "Chicken", "dd": "Duck"}}
working with JSON data
var info = Xhr.responsetext;
At this point, the JSON object is just a string, because the JSON object is returned as a string, so we are not very convenient to handle, then how can we convert to a real JS object? Here we will use the Eval () function.
Eval (' var info = ' +info);
Now that info is a real JSON object, we can easily display the data in the desired section.
var xhr = function () {var xhr = null;if (window. XMLHttpRequest) {xhr = new XMLHttpRequest ();} else if (window. ActiveXObject) {xhr = new ActiveXObject (' Microsoft.XMLHTTP ');} return XHR;} Xhr.onreadystatechange = function () { if (xhr.readystate==4 && xhr.status==200) { var info = Xhr.responsetext; Info is returned as a string to us string--' {"name": "Apple5s", "Price": "4999", "Number": "" "," Weight ":" The ""} ' //eval (' var a= {") Name ":" Apple5s "," Price ":" 4999 "} '); Eval ("var info_jn=" +info); Let info string contents run as an expression var rst = document.getElementById (' result '); rst.innerhtml + = "Name:" +info_jn.name; rst.innerhtml + = "Price:" +info_jn.price; rst.innerhtml + = "Weight:" +info_jn.weight; rst.innerhtml + = "Quantity:" +info_jn.number; }} Xhr.open ("Get", './02.php '); Xhr.send (null);