#region Newtonsoft.Json.JsonConvert 序列化
/// <summary>
/// Newtonsoft Json序列化(效率最高)
/// </summary>
/// <param name="obj">待Newtonsoft序列化的对象</param>
/// <param name="igoreNull">是否忽略值为null的字段</param>
/// <returns>Json字符串</returns>
public static string NewtonsoftJsonConvert(T obj, bool igoreNull = true)
{
if (obj == null)
return null;
try
{
var setting = new JsonSerializerSettings();
JsonConvert.DefaultSettings = () =>
{
//日期类型默认格式化处理
setting.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//空值处理
if (igoreNull)
setting.NullValueHandling = NullValueHandling.Ignore;
//Bool类型转换 设置
//setting.Converters.Add(new BoolConvert("是,否"));
return setting;
};
return JsonConvert.SerializeObject(obj, Formatting.Indented, setting);
}
catch (System.Exception)
{
return null;
}
}
/// <summary>
/// JavaScript Json反序列化(效率最高)
/// </summary>
/// <param name="jsonString">Json字符串</param>
/// <returns>JavaScript反序列化的结果</returns>
public static T NewtonsoftJsonConvert(string jsonString)
{
try
{
return JsonConvert.DeserializeObject<T>(jsonString);
}
catch (System.Exception)
{
return default(T);
}
}
/// <summary>
/// 将Json字符串转换为JObject对象(非数组用JObject加载),可支持Linq
/// </summary>
public static JObject ParseJsonObject(string jsonString)
{
return JObject.Parse(jsonString);
}
/// <summary>
/// 将Json字符串转换为JArray对象(数组用JArray加载),可支持Linq
/// JArray 只能用来查找索引的项的对象,找到对象需要转换为 JObject,再查找对应的属性值
/// </summary>
public static JArray ParseJsonArray(string jsonString)
{
return JArray.Parse(jsonString);
}
/// <summary>
/// Newtonsoft Json序列化(效率最高)
/// </summary>
/// <param name="obj">待Newtonsoft序列化的对象</param>
/// <param name="igoreNull">是否忽略值为null的字段</param>
/// <returns>Json字符串</returns>
public static string NewtonsoftJsonConvert(T obj, bool igoreNull = true)
{
if (obj == null)
return null;
try
{
var setting = new JsonSerializerSettings();
JsonConvert.DefaultSettings = () =>
{
//日期类型默认格式化处理
setting.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//空值处理
if (igoreNull)
setting.NullValueHandling = NullValueHandling.Ignore;
//Bool类型转换 设置
//setting.Converters.Add(new BoolConvert("是,否"));
return setting;
};
return JsonConvert.SerializeObject(obj, Formatting.Indented, setting);
}
catch (System.Exception)
{
return null;
}
}
/// <summary>
/// JavaScript Json反序列化(效率最高)
/// </summary>
/// <param name="jsonString">Json字符串</param>
/// <returns>JavaScript反序列化的结果</returns>
public static T NewtonsoftJsonConvert(string jsonString)
{
try
{
return JsonConvert.DeserializeObject<T>(jsonString);
}
catch (System.Exception)
{
return default(T);
}
}
/// <summary>
/// 将Json字符串转换为JObject对象(非数组用JObject加载),可支持Linq
/// </summary>
public static JObject ParseJsonObject(string jsonString)
{
return JObject.Parse(jsonString);
}
/// <summary>
/// 将Json字符串转换为JArray对象(数组用JArray加载),可支持Linq
/// JArray 只能用来查找索引的项的对象,找到对象需要转换为 JObject,再查找对应的属性值
/// </summary>
public static JArray ParseJsonArray(string jsonString)
{
return JArray.Parse(jsonString);
}
#endregion
前段:
//json反序列化
// T 可以为泛型,实体,等
var invoices = @Html.Raw(Json.Encode(T));
//对象查找方法
// guid 为对象中的方法
var details = invoices.find(function(x){return x.GUID == guid})