简体   繁体   English

反序列化为double时,JsonConvert会抛出一个“非有效整数”异常

[英]JsonConvert throws a 'not a valid integer' exception when deserializing to a double

I get an exception when I try to deserialize to an object from a JSON string. 当我尝试从JSON字符串反序列化为对象时,我得到一个异常。

Input string '46.605' is not a valid integer. Path 'LatitudeCenter'

It's really weird because JsonConvert tries to deserialize an attribute as an integer but it actually is a double and not an integer . 这真的很奇怪,因为JsonConvert尝试将属性反序列化为整数但它实际上是一个double而不是整数

I already checked in my Web API project. 我已经检查了我的Web API项目。 The attribute in my class is a double and same in web project. 我的类中的属性是web项目中的double和same。

The code I use in my web asp project: 我在web asp项目中使用的代码:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("myWebApiHostedUrl");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // Get the response
    HttpResponseMessage response = await client.GetAsync("api/NewMap/?SouthLatitude=46.600&WestLongitude=7.085&NorthLatitude=46.610&EastLongitude=7.095&Width=900&Height=900&isVoxelMap=true");
    string jsonData = response.Content.ReadAsStringAsync().Result;

    //Exception here
    NewMap dataMewMap = JsonConvert.DeserializeObject<NewMap>(jsonData, new JsonSerializerSettings() { Culture = CultureInfo.InvariantCulture,FloatParseHandling= FloatParseHandling.Double });
}

Here is my class: 这是我的班级:

public class NewMap
{
    // ...
    public double LatitudeCenter { get; set; }
    public double LongitudeCenter { get; set; }
    // ...
}

My JSON content: 我的JSON内容:

{
    // ...
    "LatitudeCenter":46.605,
    "LongitudeCenter":7.09,
    "SouthLatitude":46.6,
    "ImageBingUrl":null,
    "PercentEnvironement_Plain":0,
    // ...
}

It could very well be because your regional settings use something other than a 'dot' to represent what's after the integer part of a double , such as the fr-FR culture. 这很可能是因为您的区域设置使用了除“点”之外的其他内容来表示double精度的整数部分之后的内容,例如fr-FR文化。

A rough guess is that the JsonConvert class uses methods for parsing numbers from .NET (there's no reason why it wouldn't after all), such as Double.TryParse . 粗略的猜测是JsonConvert类使用方法来解析.NET中的数字(毕竟没有理由),例如Double.TryParse And these very method do by default, take into account your current culture . 而这些方法默认情况下做的,考虑到当前文化

Try setting the culture of JsonConvert to CultureInfo.InvariantCulture . 尝试将JsonConvert的文化设置为CultureInfo.InvariantCulture

暂无
暂无

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

相关问题 反序列化双精度类型变量时,JsonConvert引发“无效整数”异常 - JsonConvert throws a 'not a valid integer' exception when deserializing double type variable JsonConvert.DeserializeObject 在反序列化 CouchBase 响应时抛出异常 - JsonConvert.DeserializeObject throws exception when deserializing CouchBase response 将具有十六进制值的JSON反序列化为sbyte属性时,JsonConvert.DeserializeObject引发异常 - JsonConvert.DeserializeObject throws an exception when deserializing JSON with a hex value into an sbyte property 为什么反序列化到字典时JsonConvert会引发异常 - Why is JsonConvert throwing exception when deserializing to a dictionary 当成员使用 JsonConvert 反序列化两次时如何引发异常 - How to throw an exception when member comes twice on Deserializing with JsonConvert 使用 JsonConvert 反序列化 Json 文件时进行调试 - Debugging when deserializing a Json file with JsonConvert 反序列化为数据集时,JsonConvert.DeserializeObject 不起作用 - JsonConvert.DeserializeObject is not working when deserializing to a DataSet JsonConvert.DeserializeObject()抛出StackOverflow异常 - JsonConvert.DeserializeObject() throws StackOverflow exception JsonConvert.DeserializeObject <DataTable> 引发异常 - JsonConvert.DeserializeObject<DataTable> throws an exception Twitterizer引发异常:反序列化对象时发生意外的令牌:整数 - Twitterizer throwing an exception: Unexpected token when deserializing object: Integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM