可以将long类型序列化为字符串
namespace KKBM.Tools.JsonConverters { public class LongToStringConvert : JsonConverter { public override bool CanRead => base.CanRead; public override bool CanWrite => base.CanWrite; public override bool CanConvert(Type objectType) { if (objectType == typeof(long)) { return true; } if (objectType == typeof(long?)) { return true; } return false; } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken jt = JValue.ReadFrom(reader); return jt.Value<long>(); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(value.ToString()); } } }
在WebApiConfig文件中加入以下代码
//json数据 var jsonFormatter = new JsonMediaTypeFormatter(); var settings = jsonFormatter.SerializerSettings; //settings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; //settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; settings.Converters.Add(new Tools.JsonConverters.LongToStringConvert()); config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));