2024-04-30 03:56:40 发布
网友
根据the manual,Protobuf 3.0.0支持JSON序列化:
A well-defined encoding in JSON as an alternative to binary proto encoding.
我尝试了什么
json.dumps(instance)
instance.to_json()
如何将Python proto对象序列化为JSON?
在json_format模块中有一个函数MessageToJson。此函数可用于序列化消息。在
json_format
MessageToJson
我错误地安装了protobuf3-我以为它是protobuf3Python包,但它是一个非官方的python3protobuf2包,而不是相反。在你开始之前把它取下来。在
protobuf3
经过一番反复试验,以下解决方案奏效了。如果你有更好的/官方的,请随时发布。在
protobuf2
brew uninstall
protoc
protoc-3.0.0-osx-x86_64.zip
make
bin
/usr/local/bin
include
/usr/local/include
protoc version
libprotoc 3.0.0
/tmp
cd protobuf-master/python && setup.py install
相关函数是google.protobuf.json_format module中的MessageToJson:
google.protobuf.json_format module
from google.protobuf import json_format o = SomeProtobufClass() print json_format.MessageToJson(o) { ... }
在
json_format
模块中有一个函数MessageToJson
。此函数可用于序列化消息。在注意事项
我错误地安装了
protobuf3
-我以为它是protobuf3
Python包,但它是一个非官方的python3protobuf2包,而不是相反。在你开始之前把它取下来。在解决方案
经过一番反复试验,以下解决方案奏效了。如果你有更好的/官方的,请随时发布。在
先决条件:Protobuf3
protobuf2
(我使用了brew uninstall
)。确保protoc
不出现在路径中。在protoc-3.0.0-osx-x86_64.zip
。make
脚本也是一个选项。bin
目录的内容复制到/usr/local/bin
include
的内容复制到/usr/local/include
protoc version
应该显示libprotoc 3.0.0
。在Python安装
/tmp
cd protobuf-master/python && setup.py install
代码
相关函数是
google.protobuf.json_format module
中的MessageToJson
:相关问题 更多 >
编程相关推荐