zoukankan      html  css  js  c++  java
  • 基于Reflect将List泛型数据源转换为Json字符串

    在Web开发中,较为常用的数据传递格式为json键值对字符串格式,而在普遍的ORM全映射或半映射技术中,从后端所获得的数据往往是Enumerable或是List<T>类型的数据,因此做了一个通用类,实现将List<T>类型的数据向json格式数据的转换。代码如下:

    首先建立一个基于Reflect的基础类Reflect,实现对名称为给定字符串的属性的赋值或取值操作:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Collections;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Reflection;
     7 
     8 namespace ReflectionLearning
     9 {
    10     class Reflect
    11     {
    12         private Type _type;
    13         private object _obj;
    14         private PropertyInfo _propertyInfo;
    15 
    16         public Reflect(object obj)
    17         {
    18             this._obj = obj;
    19             _type = obj.GetType();            
    20         }
    21 
    22         /// <summary>
    23         /// 通过反射,传入属性名,获取属性值
    24         /// </summary>
    25         /// <param name="Name"></param>
    26         /// <returns></returns>
    27         public object GetValue(string Name)
    28         {
    29             _propertyInfo = _type.GetProperty(Name);
    30             object result = _propertyInfo.GetValue(_obj, null);
    31             return result;
    32         }
    33 
    34         /// <summary>
    35         /// 传入属性名、属性值,完成属性赋值操作
    36         /// </summary>
    37         /// <param name="Name"></param>
    38         /// <param name="value"></param>
    39         public void SetValue(string Name, object value)
    40         {
    41             _propertyInfo = _type.GetProperty(Name);
    42             _propertyInfo.SetValue(_obj,value, null);
    43         }
    44     }
    45 }

    在通用类中,对Reflect类进行引用,然后通过遍历List<T>中的每个T类的每个属性,进行键值对的转换:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Data;
     5 using System.Text;
     6 
     7 namespace ReflectionLearning
     8 {
     9     class ValueTurning
    10     {
    11         private Reflect reflect;
    12         private string[] parms;
    13         private DataTable dt;
    14         private List<Type> ResultList;
    15 
    16         //以数据源Dt,属性列表Parms,原始列表(其中元素已定义但未实例化)outList
    17         //对此类进行初始化操作
    18         public ValueTurning(DataTable Dt, string[] Parms, List<Type> outList)
    19         {
    20             dt = Dt;
    21             parms = Parms;
    22             ResultList = outList;
    23             beginTurning();
    24             GetJsonFromList(ResultList);
    25         }
    26 
    27         /// <summary>
    28         /// 遍历List中的元素,以Dt中的查询结果对其属性赋值
    29         /// </summary>
    30         public void beginTurning()
    31         {
    32             object obj;
    33             //遍历List中的每一个元素
    34             for (int i = 0; i < dt.Rows.Count; i++)
    35             {
    36                 //访问当前元素,用以初始化reflect反射类
    37                 obj = ResultList[i];
    38                 reflect = new Reflect(obj);
    39 
    40                 //遍历此元素中的每个属性,对其赋值
    41                 for (int j = 0; j < parms.Length; j++)
    42                 {
    43                     //设置属性
    44                     reflect.SetValue(parms[j], dt.Rows[i][parms[j]]);
    45                 }
    46 
    47                 TmpReset(obj);
    48             }
    49         }
    50 
    51         /// <summary>
    52         /// 以ResultList中的元素为数据源,转化为JSON字符串
    53         /// </summary>
    54         /// <returns>Json字符串</returns>
    55         public string GetJsonFromList(List<Type> ResultList)
    56         {
    57             StringBuilder strBuilder = new StringBuilder();
    58             if (ResultList.Count <= 0)
    59             {
    60                 return "";
    61             }
    62             else
    63             {
    64                 int num=ResultList.Count;
    65                 int index = 0;
    66                 object obj;
    67                 for (; index < num; index++)
    68                 {
    69                     obj = ResultList[index];
    70                     reflect = new Reflect(obj);
    71                     string parm = parms[index];
    72                     strBuilder.Append("{");
    73                     strBuilder.Append(couple(parm, reflect.GetValue(parm).ToString()));
    74                     strBuilder.Append("}");
    75                     if (index != num - 1)
    76                         strBuilder.Append(",");
    77                     TmpReset(obj);
    78                 }
    79                 return strBuilder.ToString();
    80             }
    81         }
    82 
    83         //临时对象初始化
    84         private void TmpReset(object obj)
    85         {
    86             obj = null;
    87             reflect = null;
    88         }
    89 
    90         //将键值对转化成 "str1":"str2" 形式
    91         public string couple(string str1, string str2)
    92         {
    93             return (""" + str1 + "":"" + str2 + """);
    94         }
    95     }
    96 }
  • 相关阅读:
    使用Node获取百度最新疫情信息,本地保存为json文件
    js实现将字符串首字母转换成大写
    egg实现登录鉴权(八):sequelize联表查询
    egg实现登录鉴权(七):权限管理
    egg实现登录鉴权(六):角色树的CRUD操作
    egg实现登录鉴权(五):mysql表中存储树形结构数据
    egg实现登录鉴权(四):人员新增和密码修改
    egg实现登录鉴权(三):密码的md5加密及验证
    egg实现登录鉴权(二):连接数据库(mysql)
    egg实现登录鉴权(一):生成token
  • 原文地址:https://www.cnblogs.com/cleverJoe/p/5275348.html
Copyright © 2011-2022 走看看