JsonConvert 在反序列化为 double 时抛出 'not a valid integer' 异常
JsonConvert throws a 'not a valid integer' exception when deserializing to a double
当我尝试从 JSON 字符串反序列化为对象时出现异常。
Input string '46.605' is not a valid integer. Path 'LatitudeCenter'
这真的很奇怪,因为 JsonConvert
试图 反序列化 一个属性作为一个整数,但它实际上 是一个双精度而不是一个整数.
我已经签入了我的 Web API 项目。我的 class 中的属性是 double 并且在 web 项目中相同。
我在 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 });
}
这是我的 class:
public class NewMap
{
// ...
public double LatitudeCenter { get; set; }
public double LongitudeCenter { get; set; }
// ...
}
我的JSON内容:
{
// ...
"LatitudeCenter":46.605,
"LongitudeCenter":7.09,
"SouthLatitude":46.6,
"ImageBingUrl":null,
"PercentEnvironement_Plain":0,
// ...
}
这很可能是因为您的区域设置使用了 'dot' 以外的其他东西来表示 double
的整数部分之后的内容,例如作为 fr-FR
文化。
一个粗略的猜测是 JsonConvert
class 使用方法从 .NET 解析数字(毕竟没有理由不这样做),例如 Double.TryParse。这些非常方法默认执行,考虑到您的当前文化。
尝试将 JsonConvert
的文化设置为 CultureInfo.InvariantCulture
。
我用过:
Replace(".0,","")
它对我有用
当我尝试从 JSON 字符串反序列化为对象时出现异常。
Input string '46.605' is not a valid integer. Path 'LatitudeCenter'
这真的很奇怪,因为 JsonConvert
试图 反序列化 一个属性作为一个整数,但它实际上 是一个双精度而不是一个整数.
我已经签入了我的 Web API 项目。我的 class 中的属性是 double 并且在 web 项目中相同。
我在 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 });
}
这是我的 class:
public class NewMap
{
// ...
public double LatitudeCenter { get; set; }
public double LongitudeCenter { get; set; }
// ...
}
我的JSON内容:
{
// ...
"LatitudeCenter":46.605,
"LongitudeCenter":7.09,
"SouthLatitude":46.6,
"ImageBingUrl":null,
"PercentEnvironement_Plain":0,
// ...
}
这很可能是因为您的区域设置使用了 'dot' 以外的其他东西来表示 double
的整数部分之后的内容,例如作为 fr-FR
文化。
一个粗略的猜测是 JsonConvert
class 使用方法从 .NET 解析数字(毕竟没有理由不这样做),例如 Double.TryParse。这些非常方法默认执行,考虑到您的当前文化。
尝试将 JsonConvert
的文化设置为 CultureInfo.InvariantCulture
。
我用过:
Replace(".0,","")
它对我有用