Home >Web Front-end >JS Tutorial >JQuery processes json and ajax and returns JSON example code_jquery
1. Some basic knowledge of JSON.
Objects in JSON are identified by "{}". A "{}" represents an object, such as {"AreaId":"123"}, and the value of the object is in the form of a key-value pair (key: value).
"[]", identifies the array, and the data inside the array are separated by ",", such as ["AreaId": "123", "AreaId": "345"].
In many cases it is an array of objects, that is:
In fact, an array is also an object, and the above format can also be written like this:
This represents an Area object, which has two sub-data, each sub-data is also an object, and each sub-object is an AreaId.
The definition format of strings and characters in JSON is similar to that of general C-like language definitions. Double quotes define strings and single quotes define characters.
JSON keys are enclosed in double quotes. For example, the "Area" and "AreaId" above are enclosed in double quotes. When constructing a JSON string in some languages, you can use escape Character escape double quotes.
2. JavaScript operation of JSON characters
1. First, distinguish between JSON strings and JSON objects
JSON string:
In fact, it can also be written like this:
This represents a JSON string. Since both single quotes and double quotes can represent a string in JS, the first one using double quotes and the second one using single quotes above represent a JSON string.
Look at the JSON object below
As you can see, there are no single or double quotes on the outside of the JSON object, which means it is a JSON object.
Script that broke on the server:
/* /*
Array
(
[red] => Array
(
[id] => 1
[name] => mary
)
(
[ID] = & gt; 2
[name] = & gt; Swallow
)
*/
echo json_encode($da);//The output is a string converted into json format, which can be used directly in js (as follows)
{"red":{"id" :1,"name":"mary"},"blue":{"id":2,"name":"u71d5u5b50"}}
*/
?>
Processing after returning to js:
The first one needs to use varl conversion: when it is a string, use eval to convert it into a jquery object (as follows)
The second type: does not require conversion:
There are also two methods for looping:
//Method 1:
//Method 2:
You can try the effect.
How to handle when ajax returns JSON
1. Use an ordinary aspx page to process
I think this method is the easiest to process, look at the code below