在JSON中发送NaN [英] sending NaN in json

查看:524
本文介绍了在JSON中发送NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用json.dumps()从Python将包含浮点数和NaN的数组编码为JSON字符串.

I am trying to encode an array which contains floats and NaN into JSON string from Python using json.dumps().

但是编码的JSON字符串未在PHP中成功解码. NaN是否引起此问题?我该如何解决这种情况?

But the encoded JSON string is not being decoded successfully in PHP. Is the NaN causing this problem? How can I work around this situation?

推荐答案

json.dumps具有一个allow_nan参数,默认为True.

json.dumps has an allow_nan parameter, which defaults to True.

NaN,Infinity和-Infinity不是JSON的一部分,但是它们在Javascript中是标准的,因此它们是常用的扩展名.如果收件人无法处理,请设置allow_nan=False.但是,当您尝试序列化NaN时,您将得到ValueError.

NaN, Infinity and -Infinity are not part of JSON, but they are standard in Javascript, so they're commonly used extensions. If the recipient can't handle them, set allow_nan=False. But then you'll get ValueError when you try to serialise NaN.

这篇关于在JSON中发送NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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