C#实现的json序列化和反序列化代码实例
作者:junjie 时间:2022-04-05 22:24:08
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Configuration;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.IO;
using System.Text;
namespace WebApplication1
{
//方法一:引入System.Web.Script.Serialization命名空间使用 JavaScriptSerializer类实现简单的序列化
[Serializable]
public class Person
{
private int id;
/// <summary>
/// id
/// </summary>
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
/// <summary>
/// 姓名
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
}
}
//方法二:引入 System.Runtime.Serialization.Json命名空间使用 DataContractJsonSerializer类实现序列化
//可以使用IgnoreDataMember:指定该成员不是数据协定的一部分且没有进行序列化,DataMember:定义序列化属性参数,使用DataMember属性标记字段必须使用DataContract标记类 否则DataMember标记不起作用。
[DataContract]
public class Person1
{
[IgnoreDataMember]
public int Id { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "sex")]
public string Sex { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
Person p1 = new Person();
p1.Id = 1;
p1.Name = "dxw";
Person p2 = new Person();
p2.Id = 2;
p2.Name = "wn";
List<Person> listperson = new List<Person>();
listperson.Add(p1);
listperson.Add(p2);
JavaScriptSerializer js = new JavaScriptSerializer();
//json序列化
string s = js.Serialize(listperson);
Response.Write(s);
//方法二
Person1 p11 = new Person1();
p11.Id = 1;
p11.Name = "hello";
p11.Sex = "男";
DataContractJsonSerializer json = new DataContractJsonSerializer(p11.GetType());
string szJson = "";
//序列化
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, p11);
szJson = Encoding.UTF8.GetString(stream.ToArray());
Response.Write(szJson);
}
//反序列化
//using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
//{
// DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));
// Person1 _people = (Person1)serializer.ReadObject(ms);
//}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(constr);
}
}
标签:C#,json,序列化,反序列化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Runnable和Thread实现多线程哪个更好你知道吗
2021-08-17 05:48:50
C# 数独求解算法的实现
2022-04-04 18:48:57
![](https://img.aspxhome.com/file/2023/2/68102_0s.png)
深入理解java自旋锁
2023-10-28 00:32:49
使用chatgpt实现微信聊天小程序的代码示例
2022-04-26 17:18:24
![](https://img.aspxhome.com/file/2023/0/67500_0s.jpg)
快速学习C# 设计模式之职责链模式
2022-03-28 17:47:45
![](https://img.aspxhome.com/file/2023/5/67565_0s.jpg)
Mybatis plus多租户方案的实战踩坑记录
2023-08-01 05:19:09
![](https://img.aspxhome.com/file/2023/6/57956_0s.jpg)
Java基础之详解HashSet的使用方法
2023-08-04 20:18:59
![](https://img.aspxhome.com/file/2023/7/57727_0s.png)
一文带你学会Spring JDBC的使用
2023-11-29 17:05:34
![](https://img.aspxhome.com/file/2023/7/60377_0s.jpg)
java中使用数组进行模拟加密的方法
2023-11-18 15:37:39
maven中profile的使用
2022-03-31 10:43:53
Android四种数据存储的应用方式
2023-07-25 05:01:06
基于ElasticSearch Analyzer的使用规则详解
2023-09-28 14:41:04
![](https://img.aspxhome.com/file/2023/8/63698_0s.png)
SpringBoot集成vue的开发解决方案
2023-11-24 20:58:10
![](https://img.aspxhome.com/file/2023/8/59028_0s.png)
基于Map的computeIfAbsent的使用场景和使用方式
2023-04-30 03:04:06
![](https://img.aspxhome.com/file/2023/6/63066_0s.png)
SpringBoot中实现分布式的Session共享的详细教程
2023-08-23 18:23:43
![](https://img.aspxhome.com/file/2023/6/58416_0s.jpg)
Hibernate中Session增删改查操作代码详解
2022-12-25 17:14:22
![](https://img.aspxhome.com/file/2023/6/62826_0s.jpg)
从try-with-resources到ThreadLocal,优化你的代码编写方式
2023-11-11 03:19:52
java文字转语音播报功能的实现方法
2022-05-08 18:44:41
Java进阶:Struts多模块的技巧
2023-06-18 09:40:47
java&javascript自定义加密数据传输代码示例
2021-11-29 02:09:33