【问题标题】:JSON Serialize error in python :raise TypeError(repo(o) + "is not JSON serializable")python中的JSON序列化错误:引发TypeError(repo(o)+“不是JSON可序列化”)
【发布时间】:2018-09-08 04:31:06
【问题描述】:

我有一个函数以以下形式返回输出:

结果是:

outputs {
 key: "output"
 value {
 dtype: DT_FLOAT
 tensor_shape {
 }
 float_val: -3.33661770821
 }
}

我想对这个输出进行 jsonify 处理。但是当我尝试这种方式时

def ss():
    ........
    return jsonify({'result':result})

它发送错误:

File "/usr/lib/python2.7/json/encoder.py", line 184, in default
       raise TypeError(repo(o) + "is not JSON serializable")
TypeError:outputs {
     key: "output"
     value {
     dtype: DT_FLOAT
     tensor_shape {
     }
     float_val: -3.33661770821
     }
    }

我如何对上述输出进行 jsonify 处理?或者我想单独使用 Float_val 值。

【问题讨论】:

标签: python json python-2.7 serialization set


【解决方案1】:

您可以使用

将 Tensorflow 协议消息转换为 JSON
from google.protobuf import json_format

message = my_proto_pb2.MyMessage(foo='bar')
json_string = json_format.MessageToJson(message)

参考:https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.json_format-module

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 2016-08-02
    • 2019-02-08
    • 2013-05-11
    • 1970-01-01
    相关资源
    最近更新 更多