C# JSON字串序列化與還原序列化

來源:互聯網
上載者:User

標籤:json

第一種:使用JavaScriptSerializer類,適用Web平台

C#將對象序列化成JSON字串

using System.Web.Script;using System.Web.Script.Serialization;

            JavaScriptSerializer serializer = new JavaScriptSerializer();            foreach (DataRow dr in dt.Rows)            {                Dictionary<string, string> data = new Dictionary<string, string>();                data.Add("Dev_NO", dr["Dev_NO"].ToString());                data.Add("Dev_Name", dr["Dev_Name"].ToString());                data.Add("Ol_Phone", dr["Ol_Phone"].ToString());                data.Add("Ol_IP", dr["Ol_IP"].ToString());                data.Add("Ol_Status", dr["Ol_Status"].ToString());                list.Add(data);            }            Response.Write(serializer.Serialize(list));

輸出結果為:
[{"Dev_NO":"00066666","Dev_Name":"test","Ol_Phone":"13766668888","Ol_IP":"10.196.209.174","Ol_Status":"2"},{"Dev_NO":"000F0000","Dev_Name":"test_hzlkhq","Ol_Phone":"13066668888","Ol_IP":"192.168.1.198","Ol_Status":"2"}]

如何將Json字串轉換成對象使用呢?

public class Model    {        public string Dev_NO { get; set; }        public string Dev_Name { get; set; }        public string Ol_Phone { get; set; }        public string Ol_IP { get; set; }        public int Ol_Status { get; set; }    }


string requestMes="[{\"Dev_NO\":\"00066666\",\"Dev_Name\":\"test\",\"Ol_Phone\":\"13766668888\",\"Ol_IP\":\"10.196.209.174\",\"Ol_Status\":\"2\"},{\"Dev_NO\":\"000F0000\",\"Dev_Name\":\"test_hzlkhq\",\"Ol_Phone\":\"13066668888\",\"Ol_IP\":\"192.168.1.198\",\"Ol_Status\":\"2\"}]";JavaScriptSerializer serializer = new JavaScriptSerializer();var Json = serializer.Deserialize<List<Model>>(requestMes);

第二種使用DataContractJsonSerializer,適用通用平台

需要添加引用:System.ServiceModel.Web 和 System.Runtime.Serialization,然後使用Using:

using System.Runtime.Serialization.Json;using System.Runtime.Serialization;

public class Model    {        public string Dev_NO { get; set; }        public string Dev_Name { get; set; }        public string Ol_Phone { get; set; }        public string Ol_IP { get; set; }        public int Ol_Status { get; set; }    }

    public class Test    {        public static DataTable getData()        {            var a = "{\"Dev_NO\":\"00066666\",\"Dev_Name\":\"test\",\"Ol_Phone\":\"13766668888\",\"Ol_IP\":\"10.196.209.174\",\"Ol_Status\":\"2\"}";            var serializer = new DataContractJsonSerializer(typeof(List<DTUModel>));            var mStream = new MemoryStream(Encoding.UTF8.GetBytes(a.ToString()));            List<DTUModel> list = (List<DTUModel>)serializer.ReadObject(mStream);            DataTable dt = new DataTable();            return dt;        }    }











聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.