本文介绍了当反序列化为双精度时,JsonConvert抛出“不正确的整数”异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



输入字符串'46 .605'不是一个有效的字符串整数。路径'LatitudeCenter'



真的很奇怪,因为 JsonConvert 尝试反序列化一个属性为整数,但实际上是一个double,而不是整数



我已经在我的Web API项目中检查。我的类中的属性在Web项目中是双重的和相同的。



我在我的web asp项目中使用的代码:


$ b $使用(var client = new HttpClient())
{
client.BaseAddress = new Uri(myWebApiHostedUrl); b

  
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(application / json));

//获取响应
HttpResponseMessage response = await client.GetAsync(api / NewMap /?SouthLatitude = 46.600& WestLongitude = 7.085& NorthLatitude = 46.610& EastLongitude = 7.095& ;宽度= 900安培;身高= 900安培; isVoxelMap =真);
string jsonData = response.Content.ReadAsStringAsync()。

//此处的异常
NewMap dataMewMap = JsonConvert.DeserializeObject&NewBuilder(jsonData,new JsonSerializerSettings(){Culture = CultureInfo.InvariantCulture,FloatParseHandling = FloatParseHandling.Double});
}

这是我的课程:

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

我的JSON内容: p>

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


解决方案

这可能是因为您的区域设置使用除em>'dot'表示 double 的整数部分之后的内容,例如 fr-FR 文化。



粗略的猜测是, JsonConvert 类使用从.NET解析数字的方法(有没有理由为什么它不会毕竟),如。而且这些非常方法在默认情况下执行,请考虑您当前的文化。



尝试设置 JsonConvert to CultureInfo.InvariantCulture


I get an exception when I try to deserialize to an object from a JSON string.

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.

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

The code I use in my web asp project:

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:

{
    // ...
    "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.

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.

Try setting the culture of JsonConvert to CultureInfo.InvariantCulture.

这篇关于当反序列化为双精度时,JsonConvert抛出“不正确的整数”异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:57