当我解析json后:
{
"Item1": 123456789.0123456789,
"Item2": "123456789.0123456789",
"Item3": 1.234567890123456789,
"Item4": 1234567890123456789
}
像这样:
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>());
然后我得到以下输出:
123456789,012346
123456789,0123456789
1,23456789012346
1234567890123456789
在我的实际json数据源输入中就像"name":123456789.0123456789
。如何在不丢失精度的情况下解析整个十进制值?
答案 0 :(得分:3)
您可以使用FloatParseHandling Enumeration指示其反序列化为decimal
:
JObject json = JsonConvert.DeserializeObject<JObject>(data,
new JsonSerializerSettings {
DateParseHandling = DateParseHandling.None,
FloatParseHandling = FloatParseHandling.Decimal // Added new setting
});
(你没有指定你正在使用哪个版本的Newtonsoft,所以我假设最新版本)