using System.Collections.Generic;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
namespace SysBaseLibs
{
///
/// Json操作
///
public static class Json
{
public static object ToJson(this string json)
{
return json == null ? null : JsonConvert.DeserializeObject(json);
}
public static string ToJson(this object obj)
{
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
return JsonConvert.SerializeObject(obj, timeConverter);
}
public static string ToJson(this object obj, string datetimeformats)
{
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats };
return JsonConvert.SerializeObject(obj, timeConverter);
}
public static T ToObject(this string json)
{
return json == null ? default(T) : JsonConvert.DeserializeObject(json);
}
public static List ToList(this string json)
{
return json == null ? null : JsonConvert.DeserializeObject>(json);
}
public static DataTable ToTable(this string json)
{
return json == null ? null : JsonConvert.DeserializeObject(json);
}
public static JObject ToJObject(this string json)
{
return json == null ? JObject.Parse("{}") : JObject.Parse(json.Replace(" ", ""));
}
}
}