The best way to return results after ajax is described as "Json"

Source: Internet
Author: User
Tags string to json

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

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.