首页 > 学院 > 开发设计 > 正文

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

2019-11-17 02:32:43
字体:
来源:转载
供稿:网友

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

一、 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 版本 网上有个封装好的第三方很方便

第三方下载连接:http://download.csdn.net/detail/xinping_168/4710720

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"]);        }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表