欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在c#中 Json的序列化与反序列化

程序员文章站 2022-06-15 20:34:11
...



    public static class JsonTools
    {
        #region 支持net框架4.0以上
        //DataContractJsonSerializer  需要引用System.Runtime.Serialization

        public static string ObjectToJson<T>(T obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            string json = "";
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                json = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
            }

            return json;
        }

        public static T JsonToObject<T>(string json)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            T obj = default(T);
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                obj = (T)serializer.ReadObject(ms);
                ms.Close();
            }
            return obj;
        }

        #endregion


        #region 支持net框架3.5以上
        //JavaScriptSerializer 需要引用 Sysytem.Web.Extensions

        //json序列化
        public static string ObjectToJson2<T>(T obj)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            //json序列化
            string s = js.Serialize(obj);
            return s;
        }

        public static T JsonToObject2<T>(string JsonStr)
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            T obj = Serializer.Deserialize<T>(JsonStr);
            return obj;
        }

        #endregion 


        //Bitmap转为Base64字符串
        public static string BitmapToString(Bitmap bitmap)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Seek(0, SeekOrigin.Begin);
                byte[] buffer = ms.ToArray();
                string str = System.Convert.ToBase64String(buffer);
                ms.Close();

                return str;
            }
        }

        //Base64字符串转图片
        public static Bitmap StringToBitmap(string base64Str)
        {
            if (!String.IsNullOrEmpty(base64Str))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] buffer = System.Convert.FromBase64String(base64Str);
                    ms.Write(buffer, 0, buffer.Length);
                    ms.Seek(0, SeekOrigin.Begin);
                    Bitmap bmp = new Bitmap(ms);

                    return bmp;
                }
            }

            return null;
        }
    }


关于数据类:

    public class People
    {
        public string Name = "";
        public int Age = 0;

        public List<Student> StuList = null;
    }

    public class Student
    {
        public string Num = "";
        public string Name = "";
    }

当使用DataContractJsonSerializer时是需要使用[DataContract],但是我没用也没出现问题,说是跟.net框架版本有关,如果出现了就加上试试.