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,
// ...
}
NewMap
contract, your JSON input, and that very last line of your code, everything works just fine for me. No issues here. The float is correctly recognized without an error. – Mcminn