jquery processing JSON and Ajax return JSON instance code

Source: Internet
Author: User
Tags eval json

 json data is a type of real-time data-exchange data storage method, using the most should be Ajax and JSON with the use of the following, I would introduce jquery processing JSON data method

First, some basic knowledge of JSON.   In JSON the object is identified by "{}", and a "{}" represents an object, such as {"Areaid": "123"}, and the object's value is the form of a key-value pair (key:value).     "[]", identifying an array of data within the array through "," split, such as ["Areaid": "123", "Areaid": "345"].   In many cases is an object array, that is:     code is as follows: [{"Areaid": "123"},{"Areaid": "345"}]       In fact, the array is an object, The above format can also be written like this:   code as follows: {"area": [{"Areaid": "123"},{"Areaid": "345"}]}     This represents an area object, he has two sub data, Each child data is also an object, and each child object is areaid. The definition of strings and characters in    json is similar to the general Class C language definition, with double quotes defining strings, and single quotes defining characters.   JSON's keys are enclosed in double quotes, such as "area" and "Areaid", which are enclosed in double quotes, and you can use escape character escape double quotes when constructing JSON strings in some languages.     II, JavaScript operation JSON characters   1, first distinguish the JSON string and the JSON object   JSON string: The   code is as follows: Var Strjson = "{Area": [{] Area Id ":" "123"},{"Areaid": "345"}} ",     can also be written in this way:     code is as follows: Var Strjson = ' {area ': [{' Areaid ': ' 123 '},{'] Areaid ":" 345 "}]} ',     This represents a JSON string, because single and double quotes in JS can represent a string, so the first one to use double quotes and the second to use single quotes represents a JSON string.   below look at the JSON object     Replication code code as follows: Var JSON = {"Area": [{"Areaid": "123"},{"areaId ":" 345 "}]},     See, there is no single or double quotation mark on the outside of the JSON object, which represents a JSON object.     in server-broken script:   code as follows: <?php $data [' id '] = 1; $dat [' name '] = ' Mary '; $da [' Red ']= array_merge ($data, $dat); $data 1[' id '] = 2; $dat 1[' name '] = "swallow"; $da [' Blue ']= array_merge ($data 1, $dat 1); Print_r ($DA);///printed out is a two-dimensional array (as follows)  /* Array (    [red] => array         (  &NB Sp         [id] => 1             [name] => Mary        )     [blue] => Array         (            [ID] =&G T 2             [name] => Swallow        ) */  echo Json_encode ($da )//Output is a string converted to JSON format, can be used directly in JS (below)/* {"red": {"id": 1, "name": "Mary"}, "Blue": {"id": 2, "name": "U71d5u5b50"}} * *? >       jquery script:   Return to JS after processing: the first to use the Varl transformation: is the time of the string to use eval into the jquery object (as follows) the   code is as follows: var arr =' {' red ': {' id ': 1, ' name ': ' Mary '}, ' Blue ': {' id ': 2, ' name ': ' U71d5u5b50 '} ';//u71d5u5b50 this is the automatically converted var dataobj = eval (" ("+arr+") ")//Here's the reason to add braces and double quotes. I don't know, as JSON syntax, you can only memorize   $.each (dataobj,function (idx,item) {      //output      alert (item.id+ "haha" +item.name);   })     The second: do not need to convert:   code as follows: "var arr = {" Red ": {" id ": 1," name ":" Mary "}," Blue ": {" id ": 2," name ":" U71d5u5b50 "}};   $.each (arr,function (idx,item) {        //output    alert (item.id+ "haha" +item.name ); There are two ways to     loops://Method One:     copy code code as follows: $.each (Arr,function (idx,item) {       &nbsp ;//Output    alert (item.id+ "haha" +item.name); )    //Method Two:     code is as follows: for (Var key in arr) {  alert (key);   alert (arr[key].status);  }& nbsp     Everyone can try the effect.   How Ajax returns JSON    1, using normal ASPX pages to process         I think this approach is the easiest way to look at the following code, the   code is as follows : $.ajax ({&NBsp            type: "POST",                  &NBSP ;                       URL: "default.aspx",      &NBS P                                   DAT Atype: "JSON",                              &NB Sp           success:function (data) {                &NBSP ;                                 $ ("input#s Howtime "). Val (data[0].demodata);                                         },                    &nbsp                     error:function (XMLHttpRequest, Textstatus, Errorth Rown) {                                &NB Sp                 alert (errorthrown);            &NBSP ;                            }      &N Bsp                                         This is the code for background delivery data       code as follows: Response.Clear ();    &NB Sp                     Response.Write ("[{Demodata": "This is the JSON Data" }];                          Response.Flush ();  &N Bsp       &NBSP               Response.End ();     This way of handling the data passed directly into the JSON data, that is, the foreground JS code here may be directly to parse the data into JSON object data, rather than string data, such as Data[0].demodata, This is where the JSON object data is used directly.
Related Article

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.