C#裡 JSON 序列化 與 還原序列化

來源:互聯網
上載者:User

標籤:

一、 VS2008以上版本

        /// <summary>
        ///  序列
        /// </summary>
        /// <typeparam name="T">對象類</typeparam>
        /// <param name="t">類對象</param>
        /// <returns>JSON字串</returns>
        public static string JsonSerializer<T>(T t)
        {
            // 將對象序列化為 JavaScript 物件標記法 (JSON),並將 JSON 資料還原序列化為對象。
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            // 建立其支援儲存區為記憶體的流
            MemoryStream ms = new MemoryStream();
            // 將指定對象序列化為 JavaScript 物件標記法 (JSON) 資料,並將產生的 JSON 寫入流中
            ser.WriteObject(ms, t);
            // 寫入位元組數群組轉換字串
            string tojson = Encoding.UTF8.GetString(ms.ToArray());
            // 關閉流
            ms.Close();
            // 返回JSON字元
            return tojson;
        }

        /// <summary>
        ///  還原序列化
        /// </summary>
        /// <typeparam name="T">對象類</typeparam>
        /// <param name="strJson">要反序列的JSON字串</param>
        /// <returns>返回對象</returns>
        public static T JsonDeserializer<T>(string strJson)
        {
            // 將對象序列化為 JavaScript 物件標記法 (JSON),並將 JSON 資料還原序列化為對象
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            // 建立其支援儲存區為記憶體的流
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson));
            // 以 JSON(JavaScript 物件標記法)格式讀取文檔流,並返回還原序列化的對象。
            T obj = (T)ser.ReadObject(ms);
            // 返回
            return obj;
        }

 

調用方法:

 

        private void button1_Click(object sender, EventArgs e)
        {
            student s = new student();
            List<student> stu = new List<student>();
            s.age = 11;
            s.height = 12;
            s.weight = 10;
            s.name = "tao";
            stu.Add(s);
            student s2 = new student();
            s2.age = 21;
            s2.height = 22;
            s2.weight = 20;
            s2.name = "jian";
            stu.Add(s2);
            string json = JsonHelper.JsonSerializer < List<student>>(stu);
            listBox1.Items.Add(json);
            textBox1.Text = json;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            List<student> s = JsonHelper.JsonDeserializer<List<student>>(textBox1.Text);
            listBox1.Items.Add(s[0].name);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            student s = new student();
            s.age = 11;
            s.height = 12;
            s.weight = 10;
            s.name = "tao222";
            string json = JsonHelper.JsonSerializer<student>(s);
            listBox1.Items.Add(json);
            textBox1.Text = json;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            student s = JsonHelper.JsonDeserializer<student>(textBox1.Text);
            listBox1.Items.Add(s.name);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataTable dt = getDataTable();
            string json = JsonHelper.JsonSerializer(dt);
            listBox1.Items.Add(json);
            textBox1.Text = json;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DataTable dt = JsonHelper.JsonDeserializer<DataTable>(textBox1.Text);
            listBox1.Items.Add(dt.Rows[0]["name"]);
        }

 

二 、 VS2005 版本 網上有個封裝好的第三方很方便

        private void button1_Click(object sender, EventArgs e)
        {
            student s = new student();
            List<student> stu = new List<student>();
            s.age = 11;
            s.height = 12;
            s.weight = 10;
            s.name = "tao";
            stu.Add(s);
            student s2 = new student();
            s2.age = 21;
            s2.height = 22;
            s2.weight = 20;
            s2.name = "jian";
            stu.Add(s2);
            string json = JsonConvert.SerializeObject(stu);
            listBox1.Items.Add(json);
            textBox1.Text = json;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<student> s = JsonConvert.DeserializeObject<List<student>>(textBox1.Text);
            listBox1.Items.Add(s[0].name);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            student s = new student();
            s.age = 11;
            s.height = 12;
            s.weight = 10;
            s.name = "tao";
            string json = JsonConvert.SerializeObject(s);
            listBox1.Items.Add(json);
            textBox1.Text = json;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            student s = JsonConvert.DeserializeObject<student>(textBox1.Text);
            listBox1.Items.Add(s.age);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DataTable dt = getDataTable();
            string json = JsonConvert.SerializeObject(dt);
            listBox1.Items.Add(json);
            textBox1.Text = json;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DataTable dt = JsonConvert.DeserializeObject<DataTable>(textBox1.Text);
            listBox1.Items.Add(dt.Rows[0]["name"]);
        }

 

C#裡 JSON 序列化 與 還原序列化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.