Json序列化帮助类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Json序列化帮助类相关的知识,希望对你有一定的参考价值。

最近开发的项目中需要实现.NET和android程序做接口,实现Android调用.NET服务!
中间以什么类型的数据进行传输呢,因为Android擅长于JSON类型解析,所以最终决定数据传输格式为JSON,
下面是将对象,集合,等转换为JSON字符串的帮助类!

1.首先添加程序集引用
System.Web.Extensions
System.Runtime.Serialization

2.引入命名空间
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;

3.将以下代码拷贝到一个新建类里面,新建类名叫JsonHelper

 /// <summary>
    /// JSON帮助类。用于将对象转换为Json格式的字符串,或者将Json的字符串转化为对象。
    /// </summary>
    public static class JsonHelper
    {
        /// <summary>
        /// 将对象转化为Json字符串 
        /// </summary>
        /// <typeparam name="T">源类型</typeparam>
        /// <param name="obj">源类型实例</param>
        /// <returns>Json字符串</returns>
        public static string GetJsonFromObj<T>(T obj)
        {
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                jsonSerializer.WriteObject(ms, obj);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }

        /// <summary>
        /// 将Json字符串转化为对象
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="strJson">Json字符串</param>
        /// <returns>目标类型的一个实例</returns>
        public static T GetObjFromJson<T>(string strJson)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson)))
            {
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(obj.GetType());
                return (T)jsonSerializer.ReadObject(ms);
            }
        }

        /// <summary>
        /// 将DataTable转换为JSON字符串
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <returns>JSON字符串</returns>
        public static string GetJsonFromDataTable(DataTable dt)
        {
            StringBuilder JsonString = new StringBuilder();
            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("{ ");
                JsonString.Append("\"TableInfo\":[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                        }
                    }
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("]}");
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }

        /// <summary> 
        /// 将对象转化为Json字符串 
        /// </summary> 
        /// <param name="obj">源对象</param> 
        /// <returns>json数据</returns> 
        public static string ObjToJson(this object obj)
        {
            javascriptSerializer serialize = new JavaScriptSerializer();
            return serialize.Serialize(obj);
        }

        /// <summary> 
        /// 将Json字符串转化为对象
        /// </summary> 
        /// <param name="strJson">Json字符串</param> 
        /// <returns>目标对象</returns>
        public static T JsonToObj<T>(string strJson)
        {
            JavaScriptSerializer serialize = new JavaScriptSerializer();
            return serialize.Deserialize<T>(strJson);
        }

        /// <summary> 
        /// 将对象转化为Json字符串(控制深度 )
        /// </summary> 
        /// <param name="obj">源对象</param> 
        /// <param name="recursionDepth">深度</param> 
        /// <returns>json数据</returns> 
        public static string ObjToJson(this object obj, int recursionDepth)
        {
            JavaScriptSerializer serialize = new JavaScriptSerializer();
            serialize.RecursionLimit = recursionDepth;
            return serialize.Serialize(obj);
        }

        /// <summary> 
        /// 将Json字符串转化为对象(控制深度 )
        /// </summary> 
        /// <param name="strJson">Json字符串</param> 
        /// <param name="recursionDepth">深度</param> 
        /// <returns>目标对象</returns>
        public static T JsonToObj<T>(string strJson, int recursionDepth)
        {
            JavaScriptSerializer serialize = new JavaScriptSerializer();
            serialize.RecursionLimit = recursionDepth;
            return serialize.Deserialize<T>(strJson);
        }

        /// <summary> 
        /// 将DataTable转换为JSON字符串
        /// </summary> 
        /// <param name="dt">DataTable</param> 
        /// <returns>json数据</returns> 
        public static string DataTableToJson(DataTable dt)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            int index = 0;
            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> result = new Dictionary<string, object>();

                foreach (DataColumn dc in dt.Columns)
                {
                    result.Add(dc.ColumnName, dr[dc].ToString());
                }
                dic.Add(index.ToString(), result);
                index++;
            }
            return ObjToJson(dic);
        }
    }

  

以上是关于Json序列化帮助类的主要内容,如果未能解决你的问题,请参考以下文章

将JSON反序列化为相对类

ASP.NET(C#) Json序列化反序列化帮助类Jsonhelper

RestFramework

在flutter中使用json_serializable对json对象进行反序列化

如何反序列化包含多维数组的json对象?

Json数据序列化