Google protobuf如何添加子子

Google protobuf how to add sub-children

我有以下 protobuf 定义

message test {
repeated string fruit = 1 [(order_is_significant) = true]
}

我想将苹果、梨添加到 protbuf,输出应该类似于

  "test": [
  {
    "fruit": [
    "apple",
    "pear"
  ]
  "rank": 1
   }

我尝试使用 test.addFruit("apple") 和 test.addFruit("pear") 方法添加,但输出看起来像

test {
  fruit: "apple"
  fruit: "pear"
  rank: 1
}

有什么想法吗?

您的输出是协议缓冲区文本格式。你想要的看起来像 json。因此,您似乎需要某种 json protobuf 序列化程序。

C++:https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.util.json_util

Java: https://developers.google.com/protocol-buffers/docs/reference/java/

您将在 C++ 的左窗格中找到其他语言的格式化程序 link。