Ajax processing JSON data

Source: Internet
Author: User

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);

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.