Can I Deserialize a JSON string that contains 0.0 in C#?

巧了我就是萌 提交于 2019-12-31 05:36:08

问题


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.


回答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



来源:https://stackoverflow.com/questions/42279738/can-i-deserialize-a-json-string-that-contains-0-0-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!