JsonHelper

public class JsonHelper
{
/// <summary>
/// Serialize object to Json
/// </summary>
/// <typeparam name="T">Object Type</typeparam>
/// <param name="t">Serialize object</param>
/// <returns>return string</returns>
public static string ObjectToJson<T>(T t)
{
var scriptSerializer = new JavaScriptSerializer();
scriptSerializer.MaxJsonLength = 1024000000;
string json = scriptSerializer.Serialize(t);
return json;
}
/// <summary>
/// Desrialize Json to object
/// </summary>
/// <typeparam name="T">object Type</typeparam>
/// <param name="json">json you wanna convert to special object</param>
/// <returns>return Desrialize object</returns>
public static T JsonToObject<T>(string json)
{
var scriptSerializer = new JavaScriptSerializer();
scriptSerializer.MaxJsonLength = 1024000000;
var obj = scriptSerializer.Deserialize<T>(json);
return obj;
}

/// <summary>
/// 支持WCF扩展数据序列化方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string ObjectToJson_New<T>(T t)
{
string json = JsonConvert.SerializeObject(t);
return json;
}

public static T JsonToObject_New<T>(string json)
{
var obj = JsonConvert.DeserializeObject<T>(json);
return obj;
}
}

你可能感兴趣的:(json)