我可以在C#中反序列化包含0.0的JSON字符串吗? [英] Can I Deserialize a JSON string that contains 0.0 in C#?

查看:146
本文介绍了我可以在C#中反序列化包含0.0的JSON字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从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.

推荐答案

我说你应该继续在您可以使用C#动态类型使事情变得更容易.由于不依赖魔术字符串,该技术还使重构更加简单.使用JsonConvert.DeserializeObject<dynamic>()将该字符串反序列化为动态类型,然后以通常的方式在C#中访问其属性.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆