实体类转json 泛型集合list转json
用json.net的方式。参考网址:http://www.cnblogs.com/hxling/archive/2010/10/31/1865575.html
主要的代码:
public static string ToJson(object obj) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); JsonSerializer json = new JsonSerializer(); json.Serialize(new JsonTextWriter(sw),obj); return sb.ToString(); }
泛型集合List转json
string result = string.Empty; List<ApplicationDomainCategory> list= new ApplicationDomainCategoryManager().GetAll(); result=JsonConvert.SerializeObject(list); return result;
微软自带的方式:
引用命名空间 using System.Web.Script.Serialization;
JavaScriptSerializer jss = new JavaScriptSerializer(); result = jss.Serialize(data);//data为实体类