0

The JSON I'm getting back from a webservice has an integer incorrectly represented as 0.0. My deserialization code looks like this:

var serializer = new JsonSerializer();
var ret = serializer.Deserialize<T>(jsonTextReader);

And I get an error like this:

Input string '0.0' is not a valid integer.

My question is, is there a way to specify a less strict deserialization method so that I can parse this string?

EDIT: The web service returns no schema so I don't know why the deserializer tries to convert it to an int instead of a float or double.

CamJohnson26
  • 1,119
  • 1
  • 15
  • 40
  • 1
    it is not int. Must change to Decimal/Double ? Or use diff Json deserializer, or write custom Int deserializer. They might claim it is int (meaning no digits after dot/comma) but their API sends it as a double/float/decimal. – Leszek P Feb 16 '17 at 16:37
  • Good point, but there's no schema returned from the webservice so I don't know why it's expecting an int. – CamJohnson26 Feb 16 '17 at 16:40
  • 4
    your T model/class has property int, instead of double? – Leszek P Feb 16 '17 at 16:41
  • 1
    Here's an example custom JsonConverter: http://stackoverflow.com/questions/17745866/how-can-i-restore-the-int-deserialization-behavior-after-upgrading-json-net –  Feb 16 '17 at 16:42
  • Ah yes I autogenerated these classes and it set it to int. Stupid mistake, can you post that as an answer? – CamJohnson26 Feb 16 '17 at 16:44
  • What do you want to do in the case of, say, `"0.1"` instead of `"0.0"`? – dbc Feb 16 '17 at 17:07

1 Answers1

1

I'd say that you should go ahead and creat your classes on Json -> C#

var o = (JObject)serializer.Deserialize(myjsondata);

You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings. Use JsonConvert.DeserializeObject<dynamic>()to deserialize this string into a dynamic type then simply access its properties in the usual way in C#.

Im not sure why youre getting

Input string '0.0' is not a valid integer.

since if you dont have any Json data it should just be left at null and you shouldnt have this problem

Boschko
  • 367
  • 5
  • 14