I have been using ajax for many years. After sending a request to asp or php for processing, I usually output a string and then return it to the client, split the string using the client javascript.
Xml is also used occasionally. The server generates xml after processing and then returns it.
However, these methods are difficult to handle on the client. In fact, json can be used for processing. Because json is the most flexible when operating on the client, let's look at the code below:
Assume that the string is the content that I output to the client after processing various operations from the server. On the surface, it is a string, but it follows the json format. if you want to publish this article, please indicate the location, without the right to publish the copyright, I do not like to see that kind of website my work does not indicate the person QQ9256114
'[{"Name": "Wang Qiang", "age": 20 },{ "name": "Wang haiyun", "age": 18}]'
I use a js variable on the client to receive this string.
Var str = '[{"name": "Wang Qiang", "age": 20 },{ "name": "Wang haiyun", "age": 18}]';
Then convert the string to json
Var JsonL = eval ("(" + str + ")"); // or var JsonL = new Function ("return" + str )();
// After conversion, it should be: JsonL = [{"name": "Wang Qiang", "age": 20 },{ "name": "Wang haiyun", "age ": 18}];
// It is not difficult to see that this structure is very similar to array, so we can use the idea of operating the array to operate the json
Next we can control the output of elements in json.
Alert (JsonL [1]. name );
For (var I = 0; I <JsonL. length; I ++ ){
For (var key in JsonL [I]) {
Alert ("key:" + key + ", value:" + JsonL [I] [key]);
}
}
Below is a complicated json example:
Var JsonL = [
{"Parent": 1, "id": 11, "tit": "a1", "lst": ["9 | a11", "7 | a12 ", "5 | a13", "4 | a14", "2 | a15"]},
{"Parent": 2, "id": 12, "tit": "a2", "lst": ["9 | a11", "7 | a12 ", "5 | a13", "4 | a14"]},
{"Parent": 3, "id": 13, "tit": "a3", "lst": ["9 | a11", "7 | a12 ", "5 | a13"]},
];
Alert (JsonL [0]. id );
Alert (JsonL. length );
Alert (JsonL [0]. lst. length );
Alert (JsonL [1]. parent );
// All outputs:
For (var I = 0; I <JsonL. length; I ++ ){
For (var key in JsonL [I]) {
Alert ("Alias Name:" + key + ", value:" + JsonL [I] [key]);
}
}
// Some outputs:
For (var I = 0; I <JsonL [0]. lst. length; I ++ ){
Alert ("value:" + JsonL [0]. lst [I]);
}