C#实现的json序列化和反序列化代码实例

高洛峰
发布: 2017-01-18 09:14:12
原创
1963人浏览过

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序列化和反序列化代码实例相关文章请关注php中文网!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号