[摘要]本文是对json 数据转换的讲解,对学习ASP.NET编程技术有所帮助,与大家分享。
记录json转换的几种方式:
Newtonsoft.Json.dll 插件
在项目中引用dll文件。
json 序列化:
string Jsonreslut= JsonConvert.SerializeObject(Entity);
如:
DataSet ds = schoolbll.GetList(" F_schoolLevel="+Level);
string result = JsonConvert.SerializeObject(ds);
Response.Write(result);
Response.End();
json 反序列化:
string str=“jsonstr”;
Class model = JsonConvert.DeserializeObject<Class>(jsonstr);
如:
string tasklist = Request.Params["Taskmodel"]; pt.Model.T_Task Taskmodel = JsonConvert.DeserializeObject<pt.Model.T_Task>(tasklist);字符串输出json格式:
StringBuilder sbs = new StringBuilder(); sbs.Append("{\"flag\":" + flag + ",\"msg\":\"" + msg + "\"}"); HttpContext.Current.Response.Write(sbs.ToString()); HttpContext.Current.Response.End();hashtable向页面输出json:
public void WriteJSON(Object hs, HttpResponse MyResponse) { System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); StringBuilder json = new StringBuilder(); jss.Serialize(hs, json); MyResponse.ContentType = "json"; MyResponse.Write(json.ToString()); MyResponse.End(); }