C# 对象与JSON字符串互相转换的几种方式

2021-02-17 08:19

阅读:381

随着 Rest 风格 API 的应用越来越广泛,对象与JSON字符串互相转换的功能也用的越来越多。这里介绍三种方法,期中两种为DotNet Framework .NET 3.5 及后续版本自带的 DataContractJsonSerializer、 JavaScriptSerializer,另一种为 Newtonsoft.Json.dll ,我选择它的原因是因为它支持 DotNet Framework .NET 2.0。

 

方法一、DoNet2.0 需要借助于Newtonsoft.Json.dll

我使用的版本是:Version 10.0.3

 

[csharp] view plain copy
 
        // 从一个对象信息生成Json串
        public static string ObjectToJson(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }
        // 从一个Json串生成对象信息
        public static object JsonToObject(string jsonString, object obj)
        {
            return JsonConvert.DeserializeObject(jsonString, obj.GetType());
        }

  

 

 

方法二、DataContractJsonSerializer

[csharp] view plain copy
 
        /// 
        /// Json转换成对象
        /// 
        /// 
        /// 
        /// 
        public static T JsonToObject(string jsonText)
        {
            // Framework 2.0 不支持
            DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonText));
            T obj = (T)s.ReadObject(ms);
            ms.Dispose();
            return obj;


        }


        /// 
        /// 对象转换成JSON
        /// 
        /// 
        /// 
        /// 
        public static string ObjectToJSON(T obj)
        {
            // Framework 2.0 不支持
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            string result = string.Empty;
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                ms.Position = 0;

                using (StreamReader read = new StreamReader(ms))
                {
                    result = read.ReadToEnd();
                }
            }
            return result;
        }

  

 

[csharp] view plain copy
 

 

方法三、JavaScriptSerializer

 

[csharp] view plain copy
 
public static void JsonMy(string json)
        {                     
            JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
            ToJsonMy list = js.Deserialize(json);    //将json数据转化为对象类型并赋值给list
            string result = list.result;
            string res_info = list.res_info;
            string queryorder_info = list.res_info;
        }

        public static void JsonMy4(string json)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
            ToJsonMy3 list = js.Deserialize(json);    //将json数据转化为对象类型并赋值给list
            string result = list.result;
            string res_info = list.res_info;
            string order_num = list.queryorder_info.order_num;
            List orderdetail = list.queryorder_info.orderdetail;
            string CFTUin = orderdetail[0].CFTUin;
            string HotelName = orderdetail[0].HotelName;
            string ListID = orderdetail[1].ListID;
            string State = orderdetail[2].State;
        }

  

 


评论


亲,登录后才可以留言!