Json.NET基本类型-使用小数而不是双精度 [英] Json.NET Primitive Types - use Decimal instead of Double

查看:61
本文介绍了Json.NET基本类型-使用小数而不是双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从JSON解析浮点数时,如何使JSON.NET返回小数而不是双数? (这是带有Dictionary<string,object>并已序列化的对象).

How can I make JSON.NET return a decimal instead of a double when parsing floats from JSON? (it's an object with a Dictionary<string,object> that is serialized).

我尝试编写JsonConverter,但是Can尝试使用Double类型调用CanConvert方法以尝试对其进行转换.还有其他有关重写JsonTextReader的代码,但是在最新版本的Json.Net中似乎无法实现.

I've tried writing a JsonConverter but the CanConvert method doesn't get called with a Double type in order to try and convert it. There's other code regarding overriding JsonTextReader but this doesn't seem possible in the latest versions of Json.Net.

推荐答案

在Json.NET 5.0中,Newtonsoft.Json.JsonSerializerSettings类具有新属性FloatParseHandling,您可以将此属性值设置为Newtonsoft.Json.FloatParseHandling.Decimal

In Json.NET 5.0, Newtonsoft.Json.JsonSerializerSettings class has the new property FloatParseHandling, you can set this property value Newtonsoft.Json.FloatParseHandling.Decimal

这篇关于Json.NET基本类型-使用小数而不是双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆