实体类转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为实体类

 

 

 

 

 

posted @ 2013-03-10 14:23  高山-景行  阅读(1470)  评论(0编辑  收藏  举报