2011年2月27日 于新城科技园
上午说到如何通过ajax+jquery从client端取得json数据,当然那种做法一般的项目中是不会用到的。
下午整理了一下,通过httphandler,从server侧取得数据的做法。其实也比较简单。
①、用C# 做一个Httphandler 和 HandlerFactory (其实本文中不用HandlerFactory ,直接使用Httphandler 也可以,但是习惯用Factory了)
HandlerFactory 的source:
using System; using System.Collections.Generic; using System.Text; using System.Web; namespace MyHandlers { public class MyHandlerFactory : IHttpHandlerFactory { public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { object h = null; // 省略若干无用的解析URL,直接写个class,然后使用反射,新规得到这个class string className = "MyHandlers.MyJsonHandler"; try { h = Activator.CreateInstance(Type.GetType(className)); } catch (Exception e) { throw new HttpException("error", e); } return (IHttpHandler)h; } public void ReleaseHandler(IHttpHandler handler) { } } }
Httphandler :
using System; using System.Collections.Generic; using System.Text; using System.Web; namespace MyHandlers { public class MyJsonHandler : IHttpHandler { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { Student stu1 = new Student("张三", 19, 99); Student stu2 = new Student("李四", 21, 80); Student stu3 = new Student("王五", 18, 70); List<Student> list = new List<Student>(); list.Add(stu1); list.Add(stu2); list.Add(stu3); string jsonstr = ConvertToJson.ListToJson(list, "student"); context.Response.Write(jsonstr); } } }
注意到上面有一个ConvertToJson 的类 和一个 Student的类
Student的类就不用多说了吧, 定义了姓名,年龄,成绩
using System; using System.Collections; namespace MyHandlers { public class Student { private string _name = ""; private Int32 _age; private Int32 _score; public string Name { get{return _name;} set{_name = value;} } public Int32 Age { get{return _age;} set{_age = value;} } public Int32 Score { get{return _score;} set{_score = value;} } public Student() { } public Student(string name,Int32 age,Int32 score) { this._name = name; this._age = age; this._score = score; } } }
ConvertToJson 是我在网上找到的一个用C#做JSON格式数据的方法,在这里使用一下。
using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Reflection; namespace MyHandlers { class ConvertToJson { /// <summary> /// List转成json /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonName"></param> /// <param name="list"></param> /// <returns></returns> public static string ListToJson<T>(IList<T> list, string jsonName) { StringBuilder Json = new StringBuilder(); if (string.IsNullOrEmpty(jsonName)) jsonName = list[0].GetType().Name; Json.Append("{\"" + jsonName + "\":["); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { T obj = Activator.CreateInstance<T>(); PropertyInfo[] pi = obj.GetType().GetProperties(); Json.Append("{"); for (int j = 0; j < pi.Length; j++) { Type type = pi[j].GetValue(list[i], null).GetType(); Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(pi[j].GetValue(list[i], null).ToString(), type)); if (j < pi.Length - 1) { Json.Append(","); } } Json.Append("}"); if (i < list.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return Json.ToString(); } /// <summary> /// 过滤特殊字符 /// </summary> /// <param name="s"></param> /// <returns></returns> private static string String2Json(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.Length; i++) { char c = s.ToCharArray()[i]; switch (c) { case '\"': sb.Append("\\\""); break; case '\\': sb.Append("\\\\"); break; case '/': sb.Append("\\/"); break; case '\b': sb.Append("\\b"); break; case '\f': sb.Append("\\f"); break; case '\n': sb.Append("\\n"); break; case '\r': sb.Append("\\r"); break; case '\t': sb.Append("\\t"); break; default: sb.Append(c); break; } } return sb.ToString(); } /// <summary> /// 格式化字符型、日期型、布尔型 /// </summary> /// <param name="str"></param> /// <param name="type"></param> /// <returns></returns> private static string StringFormat(string str, Type type) { if (type == typeof(string)) { str = String2Json(str); str = "\"" + str + "\""; } else if (type == typeof(DateTime)) { str = "\"" + str + "\""; } else if (type == typeof(bool)) { str = str.ToLower(); } return str; } } }
②、配置web.config, 一旦页面发生json请求,将请求指定到MyHandlerFactory。至于这个写法什么意思,或者IIS7下面应该如何配置,请自行上网搜索相关资料。
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings/> <connectionStrings/> <system.web> <compilation debug="true" /> <authentication mode="Windows" /> <httpHandlers> <add verb="*" path="*.json" type="MyHandlers.MyHandlerFactory, MyHandlers"/> </httpHandlers> </system.web> </configuration>
③、html的代码:
注意点: 解析JSON数据的时候,object对象里面的元素就是一开始在C#中定义的Student类的结构。 大小写敏感。
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="./js/jquery-1.6.2.js"></script> <title>JSON数据的取得</title> <script> $(function(){ $.ajax ({ // 下面的各个参数的设置,用法,请参照 http://api.jquery.com/jQuery.ajax/ // 当然还有其他的很多参数了,具体用法不一一列举了。 url : "MyJsonHandler/student.json", // JSON数据的地址(结合web.config一起看一下) type: 'post', // 数据传送方式 dataType: 'json', // 数据类型 data: null, // 如果取得json数据之前,需要传递参数的话,可以使用data,个人理解:data就相当于一个parameter。 contentType: 'application/json', // 返回结果为成功 success : function( data, textStatus,jqXHR ) { var studentinfo = data.student; var showstr = ""; for (var i=0; i<studentinfo.length;i++) { showstr += studentinfo[i].Name + "," + studentinfo[i].Age + "," + studentinfo[i].Score + "<br>"; } $("#showarea").html(showstr); }, // 返回结果为失败 error : function(jqXHR, textStatus, errorThrown) { alert('error'); } }); }); </script> </head> <body> <section id="showarea"> </section> </body> </html>
④、结果
和上午的结果一模一样。