简体   繁体   English

newtonsoft json仅解析16个有效数字

[英]newtonsoft json parses only 16 significant digits

When I parse following json: 当我解析以下json时:

{
    "Item1": 123456789.0123456789,
    "Item2": "123456789.0123456789",
    "Item3": 1.234567890123456789,
    "Item4": 1234567890123456789
}

like so: 像这样:

string data = "{\"Item1\":123456789.0123456789,\"Item2\":\"123456789.0123456789\",\"Item3\":1.234567890123456789,\"Item4\":1234567890123456789}";
JObject json = JsonConvert.DeserializeObject<JObject>(data, new JsonSerializerSettings { DateParseHandling = DateParseHandling.None });
Console.WriteLine(json["Item1"].Value<decimal>());
Console.WriteLine(json["Item2"].Value<decimal>());
Console.WriteLine(json["Item3"].Value<decimal>());
Console.WriteLine(json["Item4"].Value<decimal>());

then I get following output: 然后我得到以下输出:

123456789,012346
123456789,0123456789
1,23456789012346
1234567890123456789

in my actual json data source inputs are like "name":123456789.0123456789 . 在我实际的json数据源输入中,例如"name":123456789.0123456789 How do I parse its whole decimal value without losing precision? 如何在不损失精度的情况下解析其整个十进制值?

You can use the FloatParseHandling Enumeration to instruct it to deserialize to decimal : 您可以使用FloatParseHandling枚举来指示将其反序列化为decimal

JObject json = JsonConvert.DeserializeObject<JObject>(data, 
    new JsonSerializerSettings { 
        DateParseHandling = DateParseHandling.None, 
        FloatParseHandling = FloatParseHandling.Decimal // Added new setting
    });

(You don't specify which version of Newtonsoft you're using so I assume the latest) (您未指定您使用的是哪个版本的Newtonsoft,所以我认为是最新的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM