Protobuf to json in python

后端 未结 2 1004
我在风中等你
我在风中等你 2020-12-13 23:57

I have an object that I de-serialize using protobuf in Python. When I print the object it looks like a python object, however when I try to convert it to

2条回答
  •  醉酒成梦
    2020-12-14 00:24

    I'd recommend using protobuf↔json converters from google's protobuf library:

    from google.protobuf.json_format import MessageToJson
    
    jsonObj = MessageToJson(org)
    

    Refer to protobuf package API: https://developers.google.com/protocol-buffers/docs/reference/python/ (see module google.protobuf.json_format).

    Note you can also serialise the protobuf to a Dict

    from google.protobuf.json_format import MessageToDict
    dict_obj = MessageToDict(org)
    

提交回复
热议问题