如何使用Jackson将对象转换为具有前导零的整数字段的JSON? [英] How to convert object to JSON of an integer field having leading zeros using Jackson?
问题描述
当我尝试转换具有值为0000
的整数字段的对象时,
转换后的JSON包含0
而不是0000
.
When I try to convert an object having an integer field with value as 0000
,
the converted JSON contains 0
instead of 0000
.
如何配置Jackson的ObjectMapper
将0000
转换为00000
?
How can I configure Jackson's ObjectMapper
to convert 0000
to 00000
?
推荐答案
前导0表示一个八进制数.因此,JSON中的数字值不允许前导零.请参见 RFC 7159
A leading 0 indicates an octal number. Hence leading zeros are not allowed for numeric values in JSON. See the following quote from the RFC 7159:
数字表示与大多数数字表示类似 编程语言. 以10为底的数字表示为 十进制数字.它包含一个整数部分,该整数部分可能是 前缀有可选的减号,其后可以跟一个 小数部分和/或指数部分. 领先的零不是 允许.
The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed.
如果需要前导零,请考虑使用字符串.
If you need leading zeros, consider using a string.
这篇关于如何使用Jackson将对象转换为具有前导零的整数字段的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!