英文:
Is there any way to extract name of field when parsing protocol buffer?
问题
我有关于协议缓冲数据的问题。当我使用Java解析和提取要使用的数据时。
message MotherData {
SonData1 son_data1 = 10;
SonData2 son_data2 = 10;
}
message SonData1 {
string my_name = 10;
}
message SonData2 {
string my_name2 = 10;
}
我想创建一个类似于Map<String, Something>
的结构。
- put("son_data",/* 现在不关心的SomeData */)
- put("son_data2",/* 现在不关心的SomeData */)
为了创建这个映射,我需要提取键作为"son_data"和"son_data2"。所以,我尝试以以下方式提取它。
Mother mother = Mother.parseFrom(/* 包括Mother内容的字节数组输入 */);
mother.getSonData1().getDescriptorForType().getName();
mother.getSonData2().getDescriptorForType().getName();
但它返回的是"SonData1"和"SonData2",而不是"son_data1"和"son_data2"。有没有办法提取用于proto的自定义名称?
英文:
I have problem with protocol buffer data. When paring and extracting the data that I want to use with java.
message MotherData {
SonData1 son_data1 = 10;
SonData2 son_data2 = 10;
}
message SonData1 {
string my_name = 10;
}
message SonData2 {
string my_name2 = 10;
}
And I want to make Map<String, Something> like this.
- put("son_data" ,/* SomeData don't care for now */)
- put("son_data2",/* SomeData don't care for now */)
To make this map I need to extract key as "son_data", "son_data2". So, I try to extract it like this way.
Mother mother = Mother.parsefrom(/*Some byte array input including contents of Mother*/);
mother.getSonData1().getDescriptorForType().getName();
mother.getSonData2().getDescriptorForType().getName();
But it returns "SonData1", "SonData2" not "son_data1", "son_data2"
Is there any way to extract own's name used in proto?
答案1
得分: 1
for (Descriptors.FieldDescriptor field : Mother.getDescriptor().getFields()) {
map.put(field.getName(), mother.getField(field));
}
英文:
for (Descriptors.FieldDescriptor field : Mother.getDescriptor().getFields()) {
map.put(field.getName(), mother.getField(field));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论