JsonConvert json.net 中带小数的本地化数字
我正在使用 json.net 来序列化和反序列化对象。唯一的问题是,当我尝试反序列化该对象时,它无法解析刚刚创建的 double 值。序列化时,它会生成十进制字符 .
,但是当它尝试反序列化时,它需要 ,
这里的问题可能是我在应用程序中运行反序列化一个设备。并且设备可能设置为与创建 JSON 的服务器不同的本地化。
那么,无论本地化格式是什么,是否可以强制 json.net
将 double
与 .
反序列化为十进制字符?
编辑 json.net codeplex 中存在一个错误,但事实恰恰相反。我尝试将我的类型从 double
do decimal
切换,现在它完美地工作...
edit2:
class Spot
{
int Id;
string Name;
double Distance;
double Rating;
}
我猜默认区域性是系统文化,对吗?这将是挪威语(nb-NO
)
这是输出。 如您所见,我将使用 ,
发送 inn double 来获取经纬度值,它们在服务中被解析为 double
。
I'm using json.net
to serialize and deserialize an object. The only problem is that when I try to deserialize the object, it fails to parse the double
values it has just created. When serializing, it makes the decimal character .
, but when it tries to deserialize, it expects ,
The issue here might be that I'm running the deserialization in an app on a device. And the device might be set to a different localization than the server that's creating the JSON.
So is it possible to force json.net
to deserialize double
with .
as a decimal character, no matter what the localized formatting might be?
edit There was a bug on this in the json.net codeplex, but it was the opposite. I tried switching my type from double
do decimal
, and now it works perfectly...
edit2:
class Spot
{
int Id;
string Name;
double Distance;
double Rating;
}
And I guess the default culture is the system culture, right? Which would be norwegian(nb-NO
)
Here's the output. And as you can see, I'm sending inn doubles with ,
for latlong values, which are parsed as double
in the service.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来这可能是 Json.NET 中的一个错误。
根据 JSON 规范,小数点分隔符应始终为
.
(参见第 2.4 节)。话虽如此,我不知道 Json.NET 可能允许您覆盖尝试反序列化时使用的区域性。
It sounds like this might be a bug in Json.NET.
The decimal separator should always be
.
according to the JSON spec (see section 2.4).Having said that, it's possible -- I don't know -- that Json.NET might allow you to override the culture that's used when attempting to deserialise.