在使用json.dumps时遇到报错TypeError: Object of type 'float32' is not JSON serializable
2018-01-02 17:43
696 查看
1down vote | This is not supported by default, but you can make it work quite easily! There are several things you'll want to encode if you want the exact same data back:The data itself, which you can get with obj.tolist()as @travelingbones mentioned. Sometimes this may be good enough. The data type. I feel this is important in quite some cases. The dimension (not necessarily 2D), which could be derived from the above if you assume the input is indeed always a 'rectangular' grid. The memory order (row- or column-major). This doesn't often matter, but sometimes it does (e.g. performance), so why not save everything? Furthermore, your numpy array could part of your data structure, e.g. you have a list with some matrices inside. For that you could use a custom encoder which basically does the above. |
class NumpyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj)a = np.array([1, 2, 3])print(json.dumps({'aa': [2, (2, 3, 4), a], 'bb': [2]}, cls=NumpyEncoder)){"aa": [2, [2, 3, 4], [1, 2, 3]], "bb": [2]}2、
自己写一个encoder去继承jsonencoder |
TypeError: Object of type 'float32' is not JSON serializable。
google后找到方法 Convert
numpy type to python。
1234567891011121314 | class MyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, numpy.integer): return int(obj) elif isinstance(obj, numpy.floating): return float(obj) elif isinstance(obj, numpy.ndarray): return obj.tolist() else: return super(MyEncoder, self).default(obj)json.dumps(numpy.float32(1.2), cls=MyEncoder)json.dumps(numpy.arange(12), cls=MyEncoder)json.dump({'a': numpy.int32(42)},fp,cls=MyEncoder) |
np.int/
np.float/
np.array这样的数据格式不支持json
serializable,而python自身的
int/
float/
list是支持的。#
python #
json_dumps
1down vote | This is not supported by default, but you can make it work quite easily! There are several things you'll want to encode if you want the exact same data back:The data itself, which you can get with obj.tolist()as @travelingbones mentioned. Sometimes this may be good enough.The data type. I feel this is important in quite some cases.The dimension (not necessarily 2D), which could be derived from the above if you assume the input is indeed always a 'rectangular' grid.The memory order (row- or column-major). This doesn't often matter, but sometimes it does (e.g. performance), so why not save everything?Furthermore, your numpy array could part of your data structure, e.g. you have a list with some matrices inside. For that you could use a custom encoder which basically does the above. |
相关文章推荐
- TypeError: Object of type 'float32' is not JSON serializable
- python2.7web代码用python3.6运行时,浏览器发送请求报错TypeError: Object of type 'bytes' is not JSON serializable的解决办法
- TypeError: Object of type 'bytes' is not JSON serializable
- 【已解决】Object of type 'QuerySet' is not JSON serializable
- 【已解决】Object of type 'range' is not JSON serializable
- Flask 中使用 JSON 响应页面的 Ajax 请求报错 TypeError: 'dict' object is not callable
- TypeError: url.indexOf is not a function ,使用jquery load()出现错误
- Spring Mvc使用Jackson进行json转对象时,遇到的字符串转日期的异常处理(Can not deserialize value of type Date from String)
- TypeError: datetime.datetime(2016, 9, 25, 21, 12, 19, 135649) is not JSON serializable解决办法(json无法序列化对象的解决办法)
- python3中,socket使用send函数时出现的错误:TypeError: a bytes-like object is required, not 'str'
- datetimeTypeError: datetime.datetime(2015, 1, 29, 8, 17, 41) is not JSON serializable
- Scrapy使用shell命令报错scrape shell TypeError: 'float' object is not iterable
- django object is not JSON serializable error问题解决
- 《Django学习》遇到的问题:“TypeError:'str' object is not callable"
- 使用apache commons pool时遇到IllegalStateException:returned object is not part of this pool
- 关于 "Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function" 的解决方案
- json.dump throwing “TypeError: {…} is not JSON serializable”
- [scrapy] exceptions.TypeError:XXX is not json serializable
- TypeError: object of type 'zip' has no len()、'zip' object is not subscriptable
- TypeError: ObjectId('') is not JSON serializable