【问题标题】:How to serialize an object如何序列化一个对象
【发布时间】:2021-11-10 11:02:14
【问题描述】:

我有一个方法可以进行一些计算,并且必须返回一个字典,如下面的代码部分所示。 该方法无法返回字典,因为它包含非序列化数据。 我尝试按照以下链接中的说明对其进行编码 https://pynative.com/online-python-code-editor-to-execute-python-code/ 还有她 TypeError: Object of type 'float32' is not JSON serializable

但我仍然收到相同的错误消息。 请告诉我如何解决这个问题

代码

resultsDict = {
    "pixelsValuesSatisfyThresholdInWindowSegment":json.dumps(numpyData,cls=NumpyArrayEncoder),
    ...
    ...
    ...
}
return resultsDict

错误信息

raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type float32 is not JSON serializable

【问题讨论】:

  • 也许你会找到一些东西here
  • 分享NumpyArrayEncodernumpyData

标签: python json numpy


【解决方案1】:

我刚刚做了以下

resultsDict = 
 {"pixelsValuesSatisfyThresholdInWindowSegment":str(pixelsValuesSatisfyThresholdInWindowSegment)
}

它正在工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多