zoukankan      html  css  js  c++  java
  • 多条Json数据转换为泛型数据

    /// <summary>
    /// 单条json数据转换为实体
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="str">字符窜(格式为{a:'',b:''})</param>
    /// <returns></returns>
    private static T ConvertToEntity<T>(string str)
    {
    Type t = typeof(T);
    object obj = Activator.CreateInstance(t);
    var properties = t.GetProperties();
    string m = str.Trim('{').Trim('}');
    string[] arr = m.Split(',');
    for (int i = 0; i < arr.Count(); i++)
    {

    for (int k = 0; k < properties.Count(); k++)
    {
    string Name = arr[i].Substring(0, arr[i].IndexOf(":"));
    object Value = arr[i].Substring(arr[i].IndexOf(":") + 1);
    if (properties[k].Name.Equals(Name))
    {
    if (properties[k].PropertyType.Equals(typeof(int)))
    {
    properties[k].SetValue(obj, Convert.ToInt32(Value), null);
    }
    if (properties[k].PropertyType.Equals(typeof(string)))
    {
    properties[k].SetValue(obj, Convert.ToString(Value), null);
    }
    if (properties[k].PropertyType.Equals(typeof(long)))
    {
    properties[k].SetValue(obj, Convert.ToInt64(Value), null);
    }
    if (properties[k].PropertyType.Equals(typeof(decimal)))
    {
    properties[k].SetValue(obj, Convert.ToDecimal(Value), null);
    }
    if (properties[k].PropertyType.Equals(typeof(double)))
    {
    properties[k].SetValue(obj, Convert.ToDouble(Value), null);
    }
    if (properties[k].PropertyType.Equals(typeof(Nullable<int>)))
    {
    properties[k].SetValue(obj, Convert.ToInt32(Value), null);
    }
    if (properties[k].PropertyType.Equals(typeof(Nullable<decimal>)))
    {
    properties[k].SetValue(obj, Convert.ToDecimal(Value), null);
    }
    if (properties[k].PropertyType.Equals(typeof(Nullable<long>)))
    {
    properties[k].SetValue(obj, Convert.ToInt64(Value), null);
    }
    if (properties[k].PropertyType.Equals(typeof(Nullable<double>)))
    {
    properties[k].SetValue(obj, Convert.ToDouble(Value), null);
    }
    if (properties[k].PropertyType.Equals(typeof(Nullable<DateTime>)))
    {
    properties[k].SetValue(obj, Convert.ToDateTime(Value), null);
    }

    }
    }

    }
    return (T)obj;
    }

    /// <summary>
    /// 多条Json数据转换为泛型数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="jsonArr">字符窜(格式为[{a:'',b:''},{a:'',b:''},{a:'',b:''}])</param>
    /// <returns></returns>
    public static List<T> ConvertTolist<T>(string jsonArr)
    {
    if (!string.IsNullOrEmpty(jsonArr) && jsonArr.StartsWith("[") && jsonArr.EndsWith("]"))
    {
    Type t = typeof(T);
    var proPerties = t.GetProperties();
    List<T> list = new List<T>();
    string recive = jsonArr.Trim('[').Trim(']').Replace("'", "").Replace(""", "");
    string[] reciveArr = recive.Replace("},{", "};{").Split(';');
    foreach (var item in reciveArr)
    {
    T obj = ConvertToEntity<T>(item);
    list.Add(obj);
    }
    return list;
    }
    return null;

    }

    用法: List<RainModel> RainModelList = ConvertTolist<RainModel>(resuledata);

  • 相关阅读:
    Nginx 模块开发(1)—— 一个稍稍能说明问题模块开发 Step By Step 过程
    nginx上传模块—nginx upload module-
    解决nginx上传模块nginx_upload_module传递GET参数
    nginx upload module的使用
    Nginx Upload Module 上传模块
    产品需求分析神器:KANO模型分析法
    SpringBoot @ConditionalOnBean、@ConditionalOnMissingBean注解源码分析与示例
    BAT、网易、京东等如何做大数据风控的?
    springboot 2.x 集成 drools 7.x
    Drools介绍与使用
  • 原文地址:https://www.cnblogs.com/fyy1003668657/p/4674246.html
Copyright © 2011-2022 走看看