1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using Newtonsoft.Json;
- using Newtonsoft.Json.Converters;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace CZFW.Core.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<T>(this string json)
- {
- return json == null ? default(T) : JsonConvert.DeserializeObject<T>(json);
- }
- public static List<T> ToList<T>(this string json)
- {
- return json == null ? null : JsonConvert.DeserializeObject<List<T>>(json);
- }
- public static DataTable ToTable(this string json)
- {
- return json == null ? null : JsonConvert.DeserializeObject<DataTable>(json);
- }
- public static JObject ToJObject(this string json)
- {
- return json == null ? JObject.Parse("{}") : JObject.Parse(json.Replace(" ", ""));
- }
- public static T Deserialize<T>(string jsonString)
- {
- return JsonConvert.DeserializeObject<T>(jsonString);
- //return new JavaScriptSerializer
- //{
- // MaxJsonLength = int.MaxValue
- //}.Deserialize<T>(jsonString);
- }
- public static string Serialize(object obj)
- {
- return ToJson(obj);
- //return Regex.Replace(new JavaScriptSerializer
- //{
- // MaxJsonLength = int.MaxValue
- //}.Serialize(obj), @"\\/Date\((\d+)\)\\/", match =>
- //{
- // DateTime dt = new DateTime(1970, 1, 1);
- // dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
- // dt = dt.ToLocalTime();
- // return dt.ToString("yyyy-MM-dd HH:mm:ss");
- //});
- }
- }
- }
|