Meh*_*ard 8 c# double json deserialization asp.net-web-api
当我尝试从JSON字符串反序列化为对象时,我得到一个异常.
Input string '46.605' is not a valid integer. Path 'LatitudeCenter'
这真的很奇怪,因为JsonConvert尝试将属性反序列化为整数但它实际上是一个double而不是整数.
我已经检查了我的Web API项目.我的类中的属性是web项目中的double和same.
我在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 });
}
Run Code Online (Sandbox Code Playgroud)
这是我的班级:
public class NewMap
{
// ...
public double LatitudeCenter { get; set; }
public double LongitudeCenter { get; set; }
// ...
}
Run Code Online (Sandbox Code Playgroud)
我的JSON内容:
{
// ...
"LatitudeCenter":46.605,
"LongitudeCenter":7.09,
"SouthLatitude":46.6,
"ImageBingUrl":null,
"PercentEnvironement_Plain":0,
// ...
}
Run Code Online (Sandbox Code Playgroud)
这很可能是因为您的区域设置使用"点"之外的其他内容来表示在整数部分之后的内容double,例如fr-FR文化.
粗略的猜测是,JsonConvert类使用方法来解析.NET中的数字(毕竟没有理由),例如Double.TryParse.而这些非常方法在默认情况下做的,考虑到你目前的文化.
尝试设置JsonConvertto 的文化CultureInfo.InvariantCulture.
| 归档时间: |
|
| 查看次数: |
5141 次 |
| 最近记录: |