在JSON中发送NaN [英] sending NaN in json
问题描述
我正在尝试使用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屋!