成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)甘井子,十載網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108>using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Globalization;
namespace Common.Core.Utilities
{
/// /// JSON幫助類
/// public static class JSONHelper
{
#region 編碼
/// /// 編碼
/// /// 類型 /// 要轉(zhuǎn)換的類型數(shù)據(jù) /// 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();
//這里使用自定義日期格式,如果不使用的話,默認(rèn)是ISO8601格式 timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
return JsonConvert.SerializeObject(t, format, timeConverter, bigintConverter);
}
#endregion #region 解碼
/// /// 解碼
/// /// 類型 /// json字符串 /// 類型數(shù)據(jù) public static T Decode(string json)
{
BigintConverter bigintConverter= new BigintConverter();
return (T)JsonConvert.DeserializeObject(json, typeof(T), bigintConverter);
}
#endregion
}
#region Bigint轉(zhuǎn)換成字符串
/// /// Bigint類型轉(zhuǎn)換處理
/// 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
}
本文標(biāo)題:JSON幫助類-創(chuàng)新互聯(lián)
標(biāo)題來源:
http://weahome.cn/article/pdech.html