gpt4 book ai didi

grpc - 将 protobuf 映射表示为 json

转载 作者:行者123 更新时间:2023-12-04 13:45:15 26 4
gpt4 key购买 nike

我正在使用 grpcc ,它基于 protobuf.js,用于测试我的 grpc 服务 API。

我的 .proto文件:

message MyRequest {
string userId = 1;
map<string, string> params = 2;
}

我尝试了以下 json 正文来发送请求:
{userId : "my_user_id" , params: { name: "my_name"}}
{userId : "my_user_id" , params: [{"name":"my_name"}] }

但这会产生以下错误:
Error: Illegal value for Message.Field....MyRequest.params of type string: object (expected ProtoBuf.Map or raw object for map field)

如何将 protobuf 映射正确表示为 json?

最佳答案

正确的 json 正文如下:
{ "userId": "my_user_id", "params": { "name": "my_name" } }

您尝试做的是一组 map ,这在 protobuf 的上下文中没有任何意义。一个 map<string, string>正是 json 对象的描述,因此多个值将按以下方式表示:
{ "params": { "key1": "value1", "key2": "value2" } }

不需要数组。

关于grpc - 将 protobuf 映射表示为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50018577/

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