using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
namespace Common.system
{
public class JsonHelper
{
#region Method
///
/// 类对像转换成json格式
///
///
public static string ToJson(object t)
{
return JsonConvert.SerializeObject(
t, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include }
);
}
///
/// 类对像转换成json格式
///
///
/// 是否忽略NULL值
///
public static string ToJson(object t, bool HasNullIgnore)
{
if (HasNullIgnore)
{
return JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
}
else
{
return ToJson(t);
}
}
///
/// json格式转换
///
///
///
///
public static T JsonToObj(string strJson) where T : class
{
if (strJson != null && strJson != "") { return JsonConvert.DeserializeObject(strJson); }
return null;
}
internal static List JsonToList(string respstr)
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(respstr);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List));
List list = o as List;
return list;
}
#endregion Method
}
}