将long数字序列化为json时,转换为字符串

由于javascript中所有数字都是64位的浮点数,所以整数只能精确的表示53bit长的数字。

在从server得到的json数据中,有ID是长整数类型,在客户端根据此ID生成的link也是不准确的。

办法1是后端ViewModel中ID都改为string类型,但是这种方式会需要在后台进行大量的转换,不是很方便。

方法2:在mvc的action进行json输出的时候,将long类型的属性直接输出为字符串。

实现方法参考:

http://stackoverflow.com/questions/17369278/convert-long-number-as-string-in-the-serialization

http://stackoverflow.com/questions/26393466/how-to-add-camelcasepropertynamescontractresolver-in-startup-cs

原文地址:https://www.cnblogs.com/ddeef/p/4296577.html