C# List<T>转为Json数据

复制代码
创建ListsConvertToJson类;//添加System.Reflection.dll
复制代码
 1 public class ListsConvertToJson
 2     {
 3         public  string ConvertJson(List<object> lists)
 4         {
 5             return ConvertJson(lists, string.Empty);
 6         }
 7         public  string ConvertJson(List<object> lists, string entityName)
 8         {
 9             try
10             {
11                 string result =string.Empty;
12                 //当传进来的entityName=Emtpty的时候,随机获取lists[index]项的类型
13                 if (entityName.Equals(string.Empty))
14                 {
15                     object obj = lists[0];
16                     entityName = obj.GetType().ToString();
17                 }
18                 result = "{\"" + entityName + "\":[" ;
19                 bool flag = true;
20                 foreach (var item in lists)
21                 {
22                     if (!flag)
23                     {
24                         result = result+"," + ListIndexConvertToJson(item);
25                     }
26                     else
27                     {
28                         result = result + ListIndexConvertToJson(item) + "";
29                         flag = false;
30                     }
31                 }
32                 result =result+ "]}";
33                 return result;
34                 
35             }
36             catch (Exception ex)
37             {                
38                 throw new ApplicationException(ex.Message);
39             }
40         }
41 
42         private string ListIndexConvertToJson(object item)
43         {
44             try
45             {
46                 string result = "{";
47                 List<string> str_lists = new List<string>();
48                 str_lists = GetAllListValue(item);
49                 foreach (var str in str_lists)
50                 {
51                     if (result.Equals("{")) {
52                         result = result + str;
53                     }
54                     else
55                     {
56                         result = result + "," + str;
57                     }
58                 }
59                 return result + "}";
60             }
61             catch (Exception ex)
62             {                
63                 throw new ApplicationException(ex.Message);
64             }
65         }
66         //反射object的所有属性值
67         private List<string> GetAllListValue(object item)
68         {
69             try
70             {
71                 List<string> lists = new List<string>();
72                 PropertyInfo[] parms = item.GetType().GetProperties();
73                 foreach (var p in parms)
74                 {
75                     lists.Add("\""+p.Name.ToString()+"\":\""+p.GetValue(item,null)+"\"");
76                 }
77                 return lists;
78             }
79             catch (Exception ex)
80             {                
81                 throw new ApplicationException(ex.Message);
82             }
83         }
84 
85     }
复制代码

从数据苦衷获取List<T>数据,调用ConvertJson方法

复制代码
 1  public string downLoadData(string Department, string BelongedTo)
 2         {
 3             try
 4             {
 5                 string where = " WHERE BelongedTo = '" + BelongedTo + "'";
 6                 //浦口综合科看全站的信息
 7                 if (BelongedTo == "AJ320111-1" && Department == "综合科")
 8                 {
 9                     where = " WHERE BelongedTo = '" + BelongedTo + "'";
10                 }
11                 else if (Department != "站领导" && Department != "局长")
12                 {
13                     where = where + " AND BelongsDepartments = '" + Department + "'";
14                 }
15                 string cmdText = string.Format("select * from View_ProjectOverview" + where);
16                 DataTable table = SQLServer.ExecuteTable(cmdText);
17 
18 
19                 List<object> Entity_coll = new List<object>();
20                 List<projectover> entity_coll = new List<projectover>();
21                 if (table != null)
22                 {
23                     foreach (DataRow row in table.Rows)
24                     {
25                         NewProjectOverview Entity = new NewProjectOverview();
26                         projectover entity = new projectover(row);
27                         if (entity.isLoaded)
28                         {
29                             Entity.isLoaded = entity.isLoaded;
30                             Entity.RecordNumber = entity.RecordNumber;
31                             Entity.ProjectName = entity.ProjectName;
32                             Entity.ProjectAddress = entity.ProjectAddress;
33                             Entity.LongitudeCoordinate = entity.LongitudeCoordinate;
34                             Entity.LatitudeCoordinate = entity.LatitudeCoordinate;
35                             Entity.ProjectCategory = entity.ProjectCategory;
36                             Entity.ProjectArea = entity.ProjectArea;
37                             Entity.ProjectAcreage = entity.ProjectAcreage;
38                             Entity.ProjectPrice = entity.ProjectPrice;
39                             Entity.ProjectHierarchy = entity.ProjectHierarchy;
40                             Entity.BelongsDepartments = entity.BelongsDepartments;
41                             Entity.dangerFlag = row["dangerFlag"].ToString();
42                             Entity.LastEditTime = row["InspectDateTime"].ToString();
43                             Entity.SGDW = entity.SGDW;
44                             Entity.RemarkContents = "";
45                             Entity_coll.Add(Entity);
46                         }
47                     }
48                 }
49 
50                 ListsConvertToJson t = new ListsConvertToJson();
51                 string json = t.ConvertJson(Entity_coll, "NewProjectOverview");
52                 return json;
53 
54             }
55             catch (Exception ex)
56             {                
57                 throw new  ApplicationException(ex.Message);
58             }
59         }
复制代码

 

复制代码
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值