首页 > 代码库 > json序列化
json序列化
/// <summary>
/// JSON序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="_Object"></param>
/// <returns></returns>
public static String ObjectToJsonString<T>(this T _Object)
{
using (MemoryStream TempMemoryStream = new MemoryStream())
{
String MySerializationString = String.Empty;
DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
MyDataContractJsonSerializer.WriteObject(TempMemoryStream, _Object);
MySerializationString = Encoding.UTF8.GetString(TempMemoryStream.ToArray());
return MySerializationString;
}
}
/// <summary>
/// JSON反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="_JsonString"></param>
/// <returns></returns>
public static T JsonStringToObject<T>(this String _JsonString)
{
using (MemoryStream TempMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(_JsonString)))
{
DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
return (T)MyDataContractJsonSerializer.ReadObject(TempMemoryStream);
}
}
我就用这个的
实体类转JSON ,json转实体类
不过需要4.0版本的
或者以上的
/// JSON序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="_Object"></param>
/// <returns></returns>
public static String ObjectToJsonString<T>(this T _Object)
{
using (MemoryStream TempMemoryStream = new MemoryStream())
{
String MySerializationString = String.Empty;
DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
MyDataContractJsonSerializer.WriteObject(TempMemoryStream, _Object);
MySerializationString = Encoding.UTF8.GetString(TempMemoryStream.ToArray());
return MySerializationString;
}
}
/// <summary>
/// JSON反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="_JsonString"></param>
/// <returns></returns>
public static T JsonStringToObject<T>(this String _JsonString)
{
using (MemoryStream TempMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(_JsonString)))
{
DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
return (T)MyDataContractJsonSerializer.ReadObject(TempMemoryStream);
}
}
我就用这个的
实体类转JSON ,json转实体类
不过需要4.0版本的
或者以上的
json序列化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。