Json data conversion, json
Several Methods for json conversion:
Newtonsoft. Json. dll plugin
Reference the dll file in the project.
Json serialization:
String Jsonreslut = JsonConvert. SerializeObject (Entity );
For example:
DataSet ds = schoolbll.GetList(" F_schoolLevel="+Level); string result = JsonConvert.SerializeObject(ds); Response.Write(result); Response.End();
Json deserialization:
String str = "jsonstr ";
Class model = JsonConvert. DeserializeObject <Class> (jsonstr );
For example:
string tasklist = Request.Params["Taskmodel"]; pt.Model.T_Task Taskmodel = JsonConvert.DeserializeObject<pt.Model.T_Task>(tasklist);
Json format of string output:
StringBuilder sbs = new StringBuilder(); sbs.Append("{\"flag\":" + flag + ",\"msg\":\"" + msg + "\"}"); HttpContext.Current.Response.Write(sbs.ToString()); HttpContext.Current.Response.End();
Hashtable outputs json to the page:
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(); }