我可以在C#中反序列化包含0.0的JSON字符串吗? [英] Can I Deserialize a JSON string that contains 0.0 in C#?
问题描述
我从Web服务获取的JSON有一个错误地表示为0.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);
我收到这样的错误:
输入字符串'0.0'不是有效的整数.
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?
Web服务不返回任何模式,所以我不知道为什么解串器尝试将其转换为int而不是float或double.
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.
推荐答案
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#.
我不确定您为什么得到
输入字符串'0.0'不是有效的整数.
Input string '0.0' is not a valid integer.
因为如果您没有任何Json数据,则应该将其保留为null,就不应该出现此问题
since if you dont have any Json data it should just be left at null and you shouldnt have this problem
这篇关于我可以在C#中反序列化包含0.0的JSON字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!