为了账号安全,请及时绑定邮箱和手机立即绑定

仅从原型中提取重复的场元素

仅从原型中提取重复的场元素

Go
慕桂英3389331 2022-08-15 15:38:00
syntax = "proto3";package TestServer;service RelaySrv{    rpc UpdateGroupDetails (Group) returns (Response);}message Person{    int64 id = 1;    string name = 2;}message Group{    repeated Person persons = 1;}Go code:    var buf bytes.Buffer    m := jsonpb.Marshaler{}    err := m.Marshal(&buf, Group)在组 protobuf 消息 buf 上执行封送后,buf 变量将具有:{ “persons” : [{“id”:“1”,“name”:“sun”}, {“id”:“2”,“name”:“sam”}] }如何提取[{“id”:“1”,“name”:“sun”}, {“id”:“2”,“name”:“sam”}]从 buf 没有清空它??
查看完整描述

2 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

如果我理解正确,这就是你想要的。


    const b = `[{"id":1,"Name":"sun"}, {"id":2,"Name":"sam"}]`


    persons := []*pb.Person{}


    err := json.Unmarshal([]byte(b), &persons)

    if err != nil {

        panic(err.Error())

    }


    log.Println(persons)

    // 2021/03/06 22:34:15 [id:1 name:"sun"  id:2 name:"sam" ]


查看完整回答
反对 回复 2022-08-15
?
狐的传说

TA贡献1804条经验 获得超3个赞

不确定是否有更好的方法,但它没有封送到json结构中,而是使用编码/解码到json。ingest.stream不再抱怨了,我可以在azure-data-explorer中看到数据


    //have a json struct for Group and Person to match the protbuf message posted in the question

    var g Group

    json.NewDecoder(&buf).Decode(&g)


    var b bytes.Buffer

    for i := 0; i < len(g.Persons); i++ {

        e := json.NewEncoder(&b).Encode(&g.Persons[i])


        if e != nil {

            panic("issue marshalling protobuf")

        }

    }


查看完整回答
反对 回复 2022-08-15
  • 2 回答
  • 0 关注
  • 106 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号