protocol-buffers - 如何获取 protobuf 消息中的所有字段?

标签 protocol-buffers

这几天在用protobuf,遇到一个问题。 我想获取protobuf消息中的所有字段,我知道一个方法,使用field_count()获取消息的字段计数,然后使用函数FindFieldByNumber() 获取所有字段。但是,如果消息的字段编号是不连续的,例如:

message MyPb
{
uint32    id   =1;
int32 score    =2;
string name    =5;
uint32  high   =6;
}

那么,MyPb的字段数是4,我用

for(int i=1; i<=count; ++i)
{
    descriptor->FindFieldByNumber(i);
}

其中计数=4。

使用这个方法,我可以得到字段namehigh吗? 如果没有,有人知道更好的方法吗? 非常感谢。

最佳答案

您可以使用 descriptor->field(i) 代替 FindFieldByNumber()。请查看the documentation for the difference between these two functions .

关于protocol-buffers - 如何获取 protobuf 消息中的所有字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43604128/

相关文章:

c# - 生成 Protocol Buffers 定义

Delphi Protocol Buffer ?

javascript - Protobuf 通过 Zeromq/zwssock 到 JSMQ/ProtoBuf.js 消息未完全收到

java - 如何使用protobuf编译java规范枚举

c++ - Protobuf、CodedInputStream 解析部分消息

go - 使用 google.protobuf.Timestamp 在 Go 中解析具有时区偏移的日期时间戳

vector - 是否可以在 protobuf 消息中包含向量字段以生成 Rust 结构?

protocol-buffers - grpc Protobuf 3 嵌套消息定义

python - 为什么protobuf生成带有__metaclass__属性的python类?

android - 默认情况下在gradle中为从proto文件生成的类注册一个包,而不是在proto文件中