.NET:序列化和反序列化

需要反序列化的字符串:

{
"LouPanID": "sample string 1",
"LouPanHao": "sample string 2",
"ChengShi": "sample string 3",
"LouPanMing": "sample string 4",
"LianXiRenMing": "sample string 5",
"LianXiRenDianHua": "sample string 6",
"ShouLouDiZhi": "sample string 7",
"WuYeLeiXing": "sample string 8",
"WuYeDiZhi": "sample string 9",
"ShiFouDaiLi": "sample string 10",
"DaiLiGongSi": "sample string 11",
"ShiFouYouYuShou": "sample string 12",
"ShiFouMianLiuShui": "sample string 13",
"YuShouZhengHao": "sample string 14",
"SuoShuQuYu": "sample string 15"
}

类结构:

    public class BuildingInfo
{
public string LouPanID { get; set; }
public string LouPanHao { get; set; }
public string ChengShi { get; set; }
public string LouPanMing { get; set; }
public string LianXiRenMing { get; set; }
public string LianXiRenDianHua { get; set; }
public string ShouLouDiZhi { get; set; }
public string WuYeLeiXing { get; set; }
public string WuYeDiZhi { get; set; }
public string ShiFouDaiLi { get; set; }
public string DaiLiGongSi { get; set; }
public string ShiFouYouYuShou { get; set; }
public string ShiFouMianLiuShui { get; set; }
public string YuShouZhengHao { get; set; }
public string SuoShuQuYu { get; set; }
}

使用System.Runtime.Serialization.Json.DataContractJsonSerializer序列化/反序列化

class Program
{
static void Main(string[] args)
{
//反序列化
string jsonBody = @"{
""LouPanID"": ""sample string 1"",
""LouPanHao"": ""sample string 2"",
""ChengShi"": ""sample string 3"",
""LouPanMing"": ""sample string 4"",
""LianXiRenMing"": ""sample string 5"",
""LianXiRenDianHua"": ""sample string 6"",
""ShouLouDiZhi"": ""sample string 7"",
""WuYeLeiXing"": ""sample string 8"",
""WuYeDiZhi"": ""sample string 9"",
""ShiFouDaiLi"": ""sample string 10"",
""DaiLiGongSi"": ""sample string 11"",
""ShiFouYouYuShou"": ""sample string 12"",
""ShiFouMianLiuShui"": ""sample string 13"",
""YuShouZhengHao"": ""sample string 14"",
""SuoShuQuYu"": ""sample string 15""
}";
BuildingInfo bi = new BuildingInfo();
DataContractJsonSerializer JsonSerializer = new DataContractJsonSerializer(typeof(BuildingInfo));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonBody));
bi = (BuildingInfo)JsonSerializer.ReadObject(ms);
//序列化
DataContractJsonSerializer _jsonSerializer = new DataContractJsonSerializer(typeof(BuildingInfo));
MemoryStream _ms = new MemoryStream();
_jsonSerializer.WriteObject(_ms, bi);
string _jsonBody = Encoding.UTF8.GetString(_ms.ToArray());
_ms.Close();
}
} [DataContract]
class BuildingInfo
{
[DataMember]
public string LouPanID { get; set; }
[DataMember]
public string LouPanHao { get; set; }
[DataMember]
public string ChengShi { get; set; }
[DataMember]
public string LouPanMing { get; set; }
[DataMember]
public string LianXiRenMing { get; set; }
[DataMember]
public string LianXiRenDianHua { get; set; }
[DataMember]
public string ShouLouDiZhi { get; set; }
[DataMember]
public string WuYeLeiXing { get; set; }
[DataMember]
public string WuYeDiZhi { get; set; }
[DataMember]
public string ShiFouDaiLi { get; set; }
[DataMember]
public string DaiLiGongSi { get; set; }
[DataMember]
public string ShiFouYouYuShou { get; set; }
[DataMember]
public string ShiFouMianLiuShui { get; set; }
[DataMember]
public string YuShouZhengHao { get; set; }
[DataMember]
public string SuoShuQuYu { get; set; }
}

如果序列化和反序列化的是一个集合,将bi的类型改为List<BuildingInfo>接口

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text; namespace Topsur.Test.Serialize
{
class Program
{
static void Main(string[] args)
{
//反序列化
#region jsonBody
string jsonBody = @"[
{
""LouPanID"": ""1"",
""LouPanHao"": ""sample string 2"",
""ChengShi"": ""sample string 3"",
""LouPanMing"": ""sample string 4"",
""LianXiRenMing"": ""sample string 5"",
""LianXiRenDianHua"": ""sample string 6"",
""ShouLouDiZhi"": ""sample string 7"",
""WuYeLeiXing"": ""sample string 8"",
""WuYeDiZhi"": ""sample string 9"",
""ShiFouDaiLi"": ""sample string 10"",
""DaiLiGongSi"": ""sample string 11"",
""ShiFouYouYuShou"": ""sample string 12"",
""ShiFouMianLiuShui"": ""sample string 13"",
""YuShouZhengHao"": ""sample string 14"",
""SuoShuQuYu"": ""sample string 15""
},
{
""LouPanID"": ""2"",
""LouPanHao"": ""sample string 2"",
""ChengShi"": ""sample string 3"",
""LouPanMing"": ""sample string 4"",
""LianXiRenMing"": ""sample string 5"",
""LianXiRenDianHua"": ""sample string 6"",
""ShouLouDiZhi"": ""sample string 7"",
""WuYeLeiXing"": ""sample string 8"",
""WuYeDiZhi"": ""sample string 9"",
""ShiFouDaiLi"": ""sample string 10"",
""DaiLiGongSi"": ""sample string 11"",
""ShiFouYouYuShou"": ""sample string 12"",
""ShiFouMianLiuShui"": ""sample string 13"",
""YuShouZhengHao"": ""sample string 14"",
""SuoShuQuYu"": ""sample string 15""
},
{
""LouPanID"": ""3"",
""LouPanHao"": ""sample string 2"",
""ChengShi"": ""sample string 3"",
""LouPanMing"": ""sample string 4"",
""LianXiRenMing"": ""sample string 5"",
""LianXiRenDianHua"": ""sample string 6"",
""ShouLouDiZhi"": ""sample string 7"",
""WuYeLeiXing"": ""sample string 8"",
""WuYeDiZhi"": ""sample string 9"",
""ShiFouDaiLi"": ""sample string 10"",
""DaiLiGongSi"": ""sample string 11"",
""ShiFouYouYuShou"": ""sample string 12"",
""ShiFouMianLiuShui"": ""sample string 13"",
""YuShouZhengHao"": ""sample string 14"",
""SuoShuQuYu"": ""sample string 15""
}
]";
#endregion List<BuildingInfo> bi = new List<BuildingInfo>();
DataContractJsonSerializer JsonSerializer = new DataContractJsonSerializer(typeof(List<BuildingInfo>));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonBody));
bi = (List<BuildingInfo>)JsonSerializer.ReadObject(ms);
//序列化
DataContractJsonSerializer _jsonSerializer = new DataContractJsonSerializer(typeof(List<BuildingInfo>));
MemoryStream _ms = new MemoryStream();
_jsonSerializer.WriteObject(_ms, bi);
string _jsonBody = Encoding.UTF8.GetString(_ms.ToArray());
_ms.Close(); }
} [DataContract]
class BuildingInfo
{
[DataMember]
public string LouPanID { get; set; }
[DataMember]
public string LouPanHao { get; set; }
[DataMember]
public string ChengShi { get; set; }
[DataMember]
public string LouPanMing { get; set; }
[DataMember]
public string LianXiRenMing { get; set; }
[DataMember]
public string LianXiRenDianHua { get; set; }
[DataMember]
public string ShouLouDiZhi { get; set; }
[DataMember]
public string WuYeLeiXing { get; set; }
[DataMember]
public string WuYeDiZhi { get; set; }
[DataMember]
public string ShiFouDaiLi { get; set; }
[DataMember]
public string DaiLiGongSi { get; set; }
[DataMember]
public string ShiFouYouYuShou { get; set; }
[DataMember]
public string ShiFouMianLiuShui { get; set; }
[DataMember]
public string YuShouZhengHao { get; set; }
[DataMember]
public string SuoShuQuYu { get; set; }
} }

可以封装成类

    public class JsonSerializeHelper
{
public static string Serialize<T>(T t)
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
jsonSerializer.WriteObject(ms, t);
string jsonResult = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return jsonResult;
} public static T Deserialize<T>(string jsonBody)
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonBody));
T objResult=(T)jsonSerializer.ReadObject(ms);
ms.Close();
return objResult;
}
}

.NET:序列化和反序列化的更多相关文章

  1. C# 序列化与反序列化几种格式的转换

    这里介绍了几种方式之间的序列化与反序列化之间的转换 首先介绍的如何序列化,将object对象序列化常见的两种方式即string和xml对象; 第一种将object转换为string对象,这种比较简单没 ...

  2. 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)

    在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...

  3. Java 序列化与反序列化

    1.什么是序列化?为什么要序列化? Java 序列化就是指将对象转换为字节序列的过程,而反序列化则是只将字节序列转换成目标对象的过程. 我们都知道,在进行浏览器访问的时候,我们看到的文本.图片.音频. ...

  4. C#中怎样实现序列化和反序列化

    我们想要将数据进行持久化的操作的话,也就是将数据写入到文件中,我们在C#中可以通过IO流来操作,同时也可以通过序列化来操作,本人是比较推荐使用序列化操作的 因为我们如果想要将一个对象持久化到文件中 如 ...

  5. Java序列化与反序列化

    Java序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java序列化与反序列化?本文围绕这些问题进行了探讨. 1.Java序列化与反序列化 Java序列化是指把Java对象转换为字节序列 ...

  6. XPatchLib 对象增量数据序列化及反序列化器 For .Net

    在日常的软件开发和使用过程中,我们发现同一套系统的同一配置项在不同的客户环境中是存在各种各样的差异的.在差异较为分散时,如何较好的管理这些差异,使得维护过程能够更加安全和快速,一直在这样那样的困扰着开 ...

  7. c# Json 自定义类作为字典键时,序列化和反序列化的处理方法

    一般情况下,Newtonsoft.Json.dll 对 Dictionary<int,object>.Dictionary<string,object>等序列化与反序列化都是成 ...

  8. java 对象序列化与反序列化

    Java序列化与反序列化是什么? 为什么需要序列化与反序列化? 如何实现Java序列化与反序列化? 本文围绕这些问题进行了探讨. 1.Java序列化与反序列化  Java序列化是指把Java对象转换为 ...

  9. 序列化,反序列化和transient关键字

    一.序列化和反序列化的概念 序列化:指把java对象转换为字节序列的过程. 反序列化:指把字节序列恢复为java对象的过程. 对象的序列化主要有两种用途: 1) 把对象的字节序列保存到硬盘上,通常存放 ...

  10. C#对象序列化与反序列化zz

      C#对象序列化与反序列化(转载自:http://www.cnblogs.com/LiZhiW/p/3622365.html) 1. 对象序列化的介绍........................ ...

随机推荐

  1. 如何在MapControl界面添加双击事件实现标绘及符号样式更改

    private void axMapControl1_OnDoubleClick(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnDo ...

  2. iOS开发UI篇—无限轮播(功能完善)

    iOS开发UI篇—无限轮播(功能完善) 一.自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条. 获取当前正在展示的位置. [self addNSTimer]; } -(void)addNS ...

  3. css关系选择器

    1.包含选择符(E F) 选择所有被E元素包含的F元素,中间用空格隔开示例:<!DOCTYPE html><html> <head> <meta charse ...

  4. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  5. 【jQuery】serializeArray()与serialize()的区别

    serialize()序列化表单元素为字符串,用于 Ajax 请求. serializeArray()序列化表单元素为JSON数据. 具体实例如下: 1 <!DOCTYPE html PUBLI ...

  6. PB中用oracle的存储过程返回记录集做数据源来生成数据窗口,PB会找不到此存储过程及不能正常识别存储过程的参数问题(转)

    (转)在PB中用oracle的存储过程返回记录集做数据源来生成数据窗口 首先oracle的存储过程写法与MSSQL不一样,差别比较大. 如果是返回数据集的存储过程则需要利用oracle的包来定义游标. ...

  7. AdaBoosting 3

    在学习AdaBoosting和online Boosting, 最好有bagging和boosting基础,这样看起来比较会比较顺.有空再补上. AdaBoost 算法的主要思想之一就是在训练集上维护 ...

  8. apply和call

    call和apply是定义在Function.prototype上的方法. 共同点:可以自由指定函数执行时内部this的指向 不同点:传参方式不同 call方法: 语法:call(thisObj,Ob ...

  9. UIkit框架之UIDatePicker

    1.继承链:UIcontrol:UIview:UIResponder:NSOobject 2.和uidatepicker相关联的触发事件是 UIControlEventValueChanged,当使用 ...

  10. 【RobotFramework自动化测试】RFS常用脚本

    读取后台数据文件:Import Variables | ${CURDIR}/\ABC.py 定位页面:Wait Until Keyword Succeeds | 5s | 500ms | select ...