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

时间:2021-10-16 11:40:52

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'

输入字符串“46.605”不是一个有效的整数。路径“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试图将属性反序列化为整数,但它实际上是双精度的,而不是整数。

I already checked in my Web API project. The attribute in my class is a double and same in web project.

我已经检查了我的Web API项目。我的类中的属性是web项目中的双属性。

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,
    // ...
}

1 个解决方案

#1


6  

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.

这很可能是因为您的区域设置使用了除'dot'之外的其他东西来表示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. And these very method do by default, take into account your current culture.

粗略的猜测是JsonConvert类使用的方法来解析. net中的数字(根本没有理由不这么做),比如Double.TryParse。这些方法都是默认的,考虑到你当前的文化。

Try setting the culture of JsonConvert to CultureInfo.InvariantCulture.

尝试设置JsonConvert to cultureinfo . constant culture。

#1


6  

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.

这很可能是因为您的区域设置使用了除'dot'之外的其他东西来表示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. And these very method do by default, take into account your current culture.

粗略的猜测是JsonConvert类使用的方法来解析. net中的数字(根本没有理由不这么做),比如Double.TryParse。这些方法都是默认的,考虑到你当前的文化。

Try setting the culture of JsonConvert to CultureInfo.InvariantCulture.

尝试设置JsonConvert to cultureinfo . constant culture。