.net 3.5 DataContractJsonSerializer序列化及反序列化二(json格式化)

前端之家收集整理的这篇文章主要介绍了.net 3.5 DataContractJsonSerializer序列化及反序列化二(json格式化)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一,将类格式化成字符串

TestCPS block = new TestCPS();
_Info result = new _Info();
result._State = "0";
result._Message = "0";
block.result = result;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TestCPS));
MemoryStream stram = new MemoryStream();
serializer.WriteObject(stram,block);
byte[] data = new byte[stram.Length];
stram.Position = 0;
stram.Read(data,(int)stram.Length);
string jsonString = Encoding.UTF8.GetString(data);

jsonString 就是json格式的字符串

TestCPS 类定义如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GetYJData
{

[Serializable]
public class TestCPS
{
private _Info Result;

public _Info result
{
get { return Result; }
set { Result = value; }
}
}
[Serializable]
public class _Info
{
private string State="0";
public string _State
{
get { return State; }
set { State = value; }
}
private string Message="输出信息";
public string _Message
{
get { return Message; }
set { Message = value; }
}
}
}

二,将字符串转化成类

string strdata = "{\"Result\":{\"State\":\"0\",\"Message\":\"填写验证失败时的出错提示\"}}";
var ser = new DataContractJsonSerializer(typeof(TestCPS));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(strdata));
TestCPS cpsData = (TestCPS)ser.ReadObject(ms);
string str1 = cpsData.result._Message;
string str2 = cpsData.result._State;

--------多条数据情况,即某个属性值为数组-------

string strdata="{\"result\":[{\"PlatformID\":\"11028\",\"UID\":\"100\",\"GameID\":\"1274\",\"UserName\":\"tkillerrt\",\"AddDate\":\"2011-12-13 23:59:59\",\"Amount\":\"10.0\",\"Units\":\"Y\"ItemNo\":\"13237915222477247\",\"AddWay\":\"\",\"CardNo\":\"\"}]}";

var ser = new DataContractJsonSerializer(typeof(_BaiduRegUserArr));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(strdata));
_BaiduRegUserArr cpsDataArr = (_BaiduRegUserArr)ser.ReadObject(ms);
if (cpsDataArr != null)
{
for (int i = 0; i < cpsDataArr.Result.Length; i++)
{
_BaiduRegUser model = cpsDataArr.Result[i];
}
}

BaiduRegUserArr类定义如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GetYJData
{
[Serializable]
public class _BaiduRegUserArr
{
private _BaiduRegUser[] result;

public _BaiduRegUser[] Result
{
get { return result; }
set { result = value; }
}
}


[Serializable]
public class _BaiduRegUser
{

//[{,"RegDate":"2011-12-13 16:01:00","ServerName":"","RegIP":"","CardNo":""}]
private string PlatformID;

public string PlatformID1
{
get { return PlatformID; }
set { PlatformID = value; }
}

private string GameID;

public string GameID1
{
get { return GameID; }
set { GameID = value; }
}

private string RegDate;

public string RegDate1
{
get { return RegDate; }
set { RegDate = value; }
}

private string UserName;

public string UserName1
{
get { return UserName; }
set { UserName = value; }
}

private string UID;

public string UID1
{
get { return UID; }
set { UID = value; }
}

private string ServerName;

public string ServerName1
{
get { return ServerName; }
set { ServerName = value; }
}

private string RegIP;

public string RegIP1
{
get { return RegIP; }
set { RegIP = value; }
}

private string CardNo;

public string CardNo1
{
get { return CardNo; }
set { CardNo = value; }
}
}

}

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

猜你在找的Json相关文章