序列化十进制JSON,如何四舍五入? [英] Serializing a decimal to JSON, how to round off?

查看:303
本文介绍了序列化十进制JSON,如何四舍五入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类

 公共类货币
{
公共字符串货币{搞定;组; }
公共小数金额{搞定;组; }
}

和想将其序列化到JSON。如果我使用的JavaScriptSerializer 我得到

  {货币: USD,金额:100.31000} 

由于API的我必须符合需求的JSON金额最大小数点后两位,我觉得它应该是可能以某种方式改变的JavaScriptSerializer 连载小数场的样子,但我不能找出如何。还有就是 SimpleTypeResolver 你可以在构造函数中通过,但它只能在工种,据我可以理解。该 JavaScriptConverter ,您可以通过添加RegisterConverters(.. )似乎是词典来进行。



我想获得

  {货币:美元,金额:100.31} 

我序列之后。此外,更改翻一番是出了问题。我可能需要做一些取整(100.311应成为100.31)。



有谁知道如何做到这一点?是否有可能到的JavaScriptSerializer ,可以让你控制序列化更详细?


解决方案<替代/ DIV>

在第一种情况下的 000 不碍事,该值仍然是相同的,将被反序列化的值完全相同。



在第二种情况下,会的JavaScriptSerializer不会帮你。在 JavacriptSerializer 是不应该改变的数据,因为它系列化到一个众所周知的格式,但它不提供在成员级别的数据转换(但它提供了自定义对象转换器) 。你想要的是一个转换+系列化,这是一个两阶段的任务。



两点建议:



1)使用 DataContractJsonSerializer :补充一点,舍入值另一个属性:

 公共类货币
{
公共字符串货币{得到;组; }

[IgnoreDataMember]
公共小数金额{搞定;组; }

[数据成员(NAME =金额)]
公共小数RoundedAmount {{返回Math.Round(金额,2); }}
}



2)克隆舍入的值对象:

 公共类货币
{
公共字符串货币{搞定;组; }

公共小数金额{搞定;组; }

公款CloneRounding(){
VAR OBJ =(金钱)this.MemberwiseClone();
obj.Amount = Math.Round(obj.Amount,2);
返回OBJ;
}
}

VAR roundMoney = money.CloneRounding();



我猜的 json.net 不能这样做下去,但我不是100%肯定。


I have a class

public class Money
{
    public string Currency { get; set; }
    public decimal Amount { get; set; }
}

and would like to serialize it to JSON. If I use the JavaScriptSerializer I get

{"Currency":"USD","Amount":100.31000}

Because of the API I have to conform to needs JSON amounts with maximum two decimal places, I feel it should be possible to somehow alter the way the JavaScriptSerializer serializes a decimal field, but I can't find out how. There is the SimpleTypeResolver you can pass in the constructor, but it only work on types as far as I can understand. The JavaScriptConverter, which you can add through RegisterConverters(...) seems to be made for Dictionary.

I would like to get

{"Currency":"USD","Amount":100.31}

after I serialize. Also, changing to double is out of the question. And I probably need to do some rounding (100.311 should become 100.31).

Does anyone know how to do this? Is there perhaps an alternative to the JavaScriptSerializer that lets you control the serializing in more detail?

解决方案

In the first case the 000 does no harm, the value still is the same and will be deserialized to the exact same value.

In the second case the JavascriptSerializer will not help you. The JavacriptSerializer is not supposed to change the data, since it serializes it to a well-known format it does not provide data conversion at member level (but it provides custom Object converters). What you want is a conversion + serialization, this is a two-phases task.

Two suggestions:

1) Use DataContractJsonSerializer: add another property that rounds the value:

public class Money
{
    public string Currency { get; set; }

    [IgnoreDataMember]
    public decimal Amount { get; set; }

    [DataMember(Name = "Amount")]
    public decimal RoundedAmount { get{ return Math.Round(Amount, 2); } }
}

2) Clone the object rounding the values:

public class Money 
{
    public string Currency { get; set; }

    public decimal Amount { get; set; }

    public Money CloneRounding() {
       var obj = (Money)this.MemberwiseClone();
       obj.Amount = Math.Round(obj.Amount, 2);
       return obj;
    }
}

var roundMoney = money.CloneRounding();

I guess json.net cannot do this either, but I'm not 100% sure.

这篇关于序列化十进制JSON,如何四舍五入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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