资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

JSON帮助类-创新互联

成都创新互联公司主要从事网站制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务甘井子,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108>using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; using System.Globalization; namespace Common.Core.Utilities { /// /// JSON帮助类 /// public static class JSONHelper { #region 编码 /// /// 编码 /// /// 类型 /// 要转换的类型数据 /// json字符串 public static string Encode(T t) { return Encode(t, Formatting.None); } #endregion #region 编码 /// /// 编码 /// /// /// /// /// public static string Encode(T t, Formatting format) { IsoDateTimeConverter timeConverter= new IsoDateTimeConverter(); BigintConverter bigintConverter= new BigintConverter(); //这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式 timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"; return JsonConvert.SerializeObject(t, format, timeConverter, bigintConverter); } #endregion #region 解码 /// /// 解码 /// /// 类型 /// json字符串 /// 类型数据 public static T Decode(string json) { BigintConverter bigintConverter= new BigintConverter(); return (T)JsonConvert.DeserializeObject(json, typeof(T), bigintConverter); } #endregion } #region Bigint转换成字符串 /// /// Bigint类型转换处理 /// public class BigintConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(System.Int64) || objectType == typeof(System.UInt64); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return 0; } else { IConvertible convertible= reader.Value as IConvertible; if (objectType == typeof(System.Int64)) { return convertible.ToInt64(CultureInfo.InvariantCulture); } else if (objectType == typeof(System.UInt64)) { return convertible.ToUInt64(CultureInfo.InvariantCulture); } return 0; } } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteValue("0"); } else if (value is Int64 || value is UInt64) { writer.WriteValue(value.ToString()); } else { throw new Exception("Expected Bigint value"); } } } #endregion }
网页标题:JSON帮助类-创新互联
URL地址:http://cdkjz.cn/article/pdech.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220