郑德才博客 记录学习,记录工作,学习知识分享!

C#、Asp.Net中将一个Json数据转换为泛型集合(或实体)

有需要的看一下哦!此方法针对于前端页面向后台传递Json数据集合,如:[{'a':'','b':""},....]格式的数据,然后在后台进行拆分,然后批量导入数据库

#region 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.Length; i++)

    {

        for (int k = 0; k < properties.Length; 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>(this 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;

}

#endregion

2012年7月26日 | 发布:郑德才博客 | 分类:网络文摘 | 评论:1

留言列表:

  • 搜趣软件 发布于 2012/7/26 15:23:46  回复
  • 这个函数或许以后会有用,先记下了。
    • 啊老表 发布于 2015/4/11 15:52:55  回复
    • 你好,小伙

发表留言: