DataContractJsonSerializer的序列化与反序列化

前端之家收集整理的这篇文章主要介绍了DataContractJsonSerializer的序列化与反序列化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

..

using System.Runtime.Serialization.Json;(要添加 System.ServiceModel.Web引用)

实体类

YunQiData model = new YunQiData();
model.AddDate = DateTime.Now.ToString();
model.Amount = 100;
model.PPlatformID = 100;
model.SID = 100;
model.UID = "2";


序列化与化序列化

DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(YunQiData));
using (MemoryStream ms = new MemoryStream())
{
ds.WriteObject(ms,model);
string output = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(output);

var ser = new DataContractJsonSerializer(typeof(YunQiData));
var ms2 = new MemoryStream(Encoding.UTF8.GetBytes(output));
YunQiData myClass1 = (YunQiData)ser.ReadObject(ms2);
if (myClass1 != null)
{

//doing

} }

原文链接:https://www.f2er.com/json/290783.html

猜你在找的Json相关文章