复制代码
创建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         }
复制代码

 

复制代码
Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐