Ajax get methods to display JSON data

Source: Internet
Author: User

<span style= "color: #3333FF;" > </span><span style= "color: #3333FF;" >public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Text/plain";
--Building JSON format data
String Jsonarr = "{' Id ':", ' Name ': ' Zhangsan '} ";//---1th data
String Jsonarr = "[{' id ': 1, ' name ': ' Zhang San ', ' age ': ' 20},{' id ': 2, ' name ': ' John Doe ', ' Age ': '}]";//---2nd data
Context. Response.Write (JSON);

list<people> Listperson = new list<people> ();
People person = new people ();
Person. Id = 1;
Person. Name = "Zhang San";
Person. Age = 20;
Listperson. ADD (person);
JavaScriptSerializer JSS = new JavaScriptSerializer ();//implement encapsulation of JSON data with the Javascriptserialize class
String json = JSS. Serialize (Listperson);//---The 3rd kind of data
Context. Response.Write (JSON);
}

class people
{
public int Id {get; set;}
public string Name {get; set;}
public int Age {get; set;}
}
</span>

<span style= "color: #3333FF;" > <script src= "/js/jquery-1.4.2.js" type= "Text/javascript" ></SCRIPT>
<script type= "text/ JavaScript "
$ (function () {
$.post ("/jsondata.ashx ", {}, function (data) {
/*
JSON is generally used in a small amount of data processing. Because the format is simple, operation aspect, and JS natively supports the processing function of JSON format
JSON general format is as follows: {"id": 1, "name": "Zhangsan"} or [{"id": 1, "name": "Zhangsan"},{"id": 2 , "Name": "Lisi"}] multidimensional array
*/
/*
JS Parsing json Format method "Personal method, there are more analytic methods online Check"
★eval (' (' +json+ ') ')
★jquery.parsejson (JSON)
*/
if (data! = NULL) {
/*
★eval (' (' +json+ ') ')
var Evaljson = eval (' (' + data + ‘)‘);
//"{' Id ': ' Name ': ' Zhangsan '}";//---"1th data in C # code"
//$ ("#spanJson"). HTML (Evaljson. Id + "-" + Evaljson. Name); The case and definition are identical
for (var i = 0; i < evaljson.length; i++) {//process multidimensional JSON---"2nd data in C # code"
$ ("#spanJson"). Append (Ev Aljson[i]. Id + "-" + evaljson[i]. Name+ "-" +evaljson[i]. Age+ "<br/>");
}
*/

/*★jquery.parsejson (JSON) */
var Parsej = $.parsejson (data);
for (var i = 0; i < parsej.length; i++) {//process multidimensional JSON "corresponds to 3rd data in C # code"
$ ("#spanJson"). Append (Parsej[i]. Id + "-" + parsej[i]. Name + "-" + parsej[i]. Age + "<br/>");
}
}
});
});
</script></span>

Ajax get methods to display JSON data

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.