JSON 序列化 与反序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
namespace JSONStudy
{
public class JsonSerializer
{
public static string JsonSerializerName(PersonName p)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(PersonName));
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, p);
string JsonString = Encoding.UTF8.GetString(ms.ToArray());
return JsonString;
}
public static PersonName JsonDeSerializer(string JsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(PersonName));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(JsonString));
PersonName p = (PersonName)ser.ReadObject(ms);
return p;
}
}
}