首先,建个ajax
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $.ajax({ type: "get", url: "student.ashx", dataType: 'text', success: function(result) { document.write("<strong>原json数据</strong><br/><hr/>"); document.write(result); var dataObj = eval("(" + result + ")"); //转换为json对象 document.write("<br/><br/><strong>解析后的数据</strong><hr/>"); for (var i = 0; i < dataObj.length; i++) { document.write("姓名:" + dataObj[i].Name + ",年龄:" + dataObj[i].Age + ",性别:" + dataObj[i].Sex + "<br/><hr/>"); } } }); </script>
再建个一般处理程序
DataContractJsonSerializer所在的 命名空间是这个 System.Runtime.Serialization.Json;
public class student : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json"; DBHelper manager = new DBHelper(); List<Student> students = manager.getAllStudent(); DataContractJsonSerializer json = new DataContractJsonSerializer(students.GetType()); json.WriteObject(context.Response.OutputStream, students); } public bool IsReusable { get { return false; } } }
然后是效果图