gpt4 book ai didi

python - 将 python dict 转换为 protobuf

转载 作者:行者123 更新时间:2023-12-05 03:41:33 24 4
gpt4 key购买 nike

如何将以下字典转换为 protobuf?
我必须将 protobuf 作为有效负载发送到 mqtt 代理。我正在使用 python 3.8

publish_msg = {
"token":"xxxxxxxx",
"parms":{
"fPort":8,
"data":b"MDQzYzAwMDE=",
"confirmed":False,
"devEUI":"8CF9572000023509"
}
}

我的protobuf定义如下:

syntax = "proto3";
package publish;


message DLParams{
string DevEUI = 1;
int32 FPort = 2;
bytes Data = 3;
bool Confirm = 4;
}

message DeviceDownlink {
string Token = 1;
DLParams Params = 2;
}

最佳答案

这个怎么样?

import json
from google.protobuf import json_format

# Create an empty message
dl = DeviceDownlink_pb2.DeviceDownlink()

# Get the json string for your dict
json_string = json.dumps(publish_msg)

# Put the json contents into your message
json_format.Parse(json_string, dl)

关于python - 将 python dict 转换为 protobuf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67687268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com