Processing mode ajax return JSON

JSON objects by "{}" to identify a "{}" represents an object, such as { "AreaId": "123"}, the value of the object key-value pairs (key: value).

json_encode()                                                                      

This function is mainly used for the array and the object, it is converted to the format json

json_encode 
(the PHP. 5> = 5.2.0, the PECL JSON> = 1.2.0) 

json_encode - coding of variables JSON 

Report a bug description 
string json_encode (mixed $ value [,  int $ options = 0])
Returns the value of the value as JSON 

Report a bug parameter 

value 
to be encoded value, in addition to the resource type, the data can be any type of 

function can only accept the UTF-8 encoded data 

options 
binary mask consisting of the following constants: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS , JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE. 

the Report bugs a return value 
encoding a successful string form represented in JSON or FALSE return failure.

 

() Get json object with the $ .ajax.

// in which dataType: "json", so that the data back to the format json. If you do not add the article attribute to a string is returned. String object can ( "(" + data + ")") method json turn into an object, but this method is not recommended for the data fetch operation by the above eval. Distinguish data as JSON object or a string, can print alert statement, if it is Object object it is JSON object, if the content was displayed string.

Copy the code
$.ajax( {
     type : "POST",
     url : "testjson!getJson.do",
     dataType:"json",
     cache:false,
     async:false,
     data : "",
     success : function(data) {
      //var object = eval("("+data+")");
      $.each(data.list,function(index,item){
       alert(item.trueName);
      })
     }
    });
Copy the code

 

jquery commonly used data types Json
Json data to the key and value pairs exist, colon connection, separated by commas, can store any type of data
Json definition:
    There js = {
            "one":1111,
            "two":"2222",
            3:new array(1,2,3),
            "four":{a:1,b:2,c:3},
             // [] array may be expressed, the contents of which can be taken through the index, taking the contents by json point or key index
             4:[1111,"2222",{a:4,b:5,c:6}],           
            };
//
// can define any type, which can point key value (key value must be a string), or by key index (in brackets)
// Value Method One:
    js.one;
// Value Method Two:
    js["one"];        js[3];
 
// Traverse Json
for(var k in js){
     alert(js[k]);
}

Guess you like

Origin www.cnblogs.com/mmzz3322/p/10995840.html