Net Core教程

C#中bigint 类型存储数据大于17位后js取值不精确

本文主要是介绍C#中bigint 类型存储数据大于17位后js取值不精确,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

可以将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));

 

这篇关于C#中bigint 类型存储数据大于17位后js取值不精确的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!