Python 不间断JSON格式十进制
我使用Flask Untivent 0.17作为API。API有几个小数字段。我可以通过安装simplejson成功返回小数,如下所述: 但是,当我有一个十进制字段是0时,该值是用科学符号表示的,例如Python 不间断JSON格式十进制,python,json,flask-restless,Python,Json,Flask Restless,我使用Flask Untivent 0.17作为API。API有几个小数字段。我可以通过安装simplejson成功返回小数,如下所述: 但是,当我有一个十进制字段是0时,该值是用科学符号表示的,例如0E-8。我想返回0.00000000 我正在尝试类似的东西,但无法让它工作。0仍然以科学记数法返回 class DecimalJSONEncoder(json.JSONEncoder): # Try to format the decimal # EDIT: but am now
0E-8
。我想返回0.00000000
我正在尝试类似的东西,但无法让它工作。0仍然以科学记数法返回
class DecimalJSONEncoder(json.JSONEncoder):
# Try to format the decimal
# EDIT: but am now realizing that this doesn't work
# It was serializing the decimal at all because I had
# simplejson installed.
def default(self, o):
if isinstance(o, decimal.Decimal):
return format(str(o), '.8f')
return super(DecimalJSONEncoder, self).default(o)
def init_app(app):
# .....
# Use the Flask-Restless postprocessor to format.
def postprocesor(result, **kw):
json.dumps(result, cls=DecimalJSONEncoder)
当小数为零时,如何强制小数为0.00000000