码迷,mamicode.com
首页 > Windows程序 > 详细

C#里 JSON 序列化 与 反序列化

时间:2015-03-04 16:25:40      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

一、 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 序列化 与 反序列化

标签:

原文地址:http://www.cnblogs.com/MrTao/p/4313593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!