Json序列化和反序列化
Code
namespace JsonTst
{
class Program
{
static void Main(string[] args)
{
Author au = new Author()
{
FirstName = "Tim",
LastName = "Heuer",
Detail = "略"
};
string s = SerializerToJsonString(au);
Author au2 = DeserializeToAuthor(s);
List<Author> auList = new List<Author>()
{
new Author{ FirstName="samuel",LastName="Chen",Detail="空"},
new Author{ FirstName="Jack",LastName="Wang",Detail="空"}
};
}
namespace JsonTst
{
class Program
{
static void Main(string[] args)
{
Author au = new Author()
{
FirstName = "Tim",
LastName = "Heuer",
Detail = "略"
};
string s = SerializerToJsonString(au);
Author au2 = DeserializeToAuthor(s);
List<Author> auList = new List<Author>()
{
new Author{ FirstName="samuel",LastName="Chen",Detail="空"},
new Author{ FirstName="Jack",LastName="Wang",Detail="空"}
};
}
Code
/// <summary>
/// 将对象序列化,以Json格式的字符串作为返回值
/// </summary>
/// <param name="objectToSerialize"></param>
/// <returns></returns>
public static string SerializerToJsonString(object objectToSerialize)
{
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
serializer.WriteObject(ms, objectToSerialize);
ms.Position = 0;
using (StreamReader sr=new StreamReader(ms))
{
return sr.ReadLine();
}
}
}
/// <summary>
/// 将对象序列化,以Json格式的字符串作为返回值
/// </summary>
/// <param name="objectToSerialize"></param>
/// <returns></returns>
public static string SerializerToJsonString(object objectToSerialize)
{
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
serializer.WriteObject(ms, objectToSerialize);
ms.Position = 0;
using (StreamReader sr=new StreamReader(ms))
{
return sr.ReadLine();
}
}
}
Code
/// <summary>
/// 将Json字符串对象反序列化成Author
/// </summary>
/// <param name="jsonString"></param>
/// <returns></returns>
public static Author DeserializeToAuthor(string jsonString)
{
byte[] by= Encoding.Default.GetBytes(jsonString);
using (MemoryStream ms=new MemoryStream(by))
{
DataContractJsonSerializer serizlizer =
new DataContractJsonSerializer(typeof(Author));
return (Author)serizlizer.ReadObject(ms);
}
}
/// <summary>
/// 将Json字符串对象反序列化成Author
/// </summary>
/// <param name="jsonString"></param>
/// <returns></returns>
public static Author DeserializeToAuthor(string jsonString)
{
byte[] by= Encoding.Default.GetBytes(jsonString);
using (MemoryStream ms=new MemoryStream(by))
{
DataContractJsonSerializer serizlizer =
new DataContractJsonSerializer(typeof(Author));
return (Author)serizlizer.ReadObject(ms);
}
}
Code
/// <summary>
/// 将Json字符串对象反序列化成Author
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonString"></param>
/// <returns></returns>
public static T DeserializeToAuthor<T>(string jsonString)
{
byte[] by = Encoding.Default.GetBytes(jsonString);
using (MemoryStream ms=new MemoryStream(by))
{
DataContractJsonSerializer serizlizer=
new DataContractJsonSerializer(typeof(T));
return (T)serizlizer.ReadObject(ms);
}
}
/// <summary>
/// 将Json字符串对象反序列化成Author
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonString"></param>
/// <returns></returns>
public static T DeserializeToAuthor<T>(string jsonString)
{
byte[] by = Encoding.Default.GetBytes(jsonString);
using (MemoryStream ms=new MemoryStream(by))
{
DataContractJsonSerializer serizlizer=
new DataContractJsonSerializer(typeof(T));
return (T)serizlizer.ReadObject(ms);
}
}
Code
[DataContract]
public class Author
{
[DataMember]
public string FirstName { set; get; }
[DataMember]
public string LastName { set; get; }
[DataMember]
public string Detail { set; get; }
}
[DataContract]
public class Author
{
[DataMember]
public string FirstName { set; get; }
[DataMember]
public string LastName { set; get; }
[DataMember]
public string Detail { set; get; }
}