Unmarshaling nested JSON objects

后端 未结 8 1811
轻奢々
轻奢々 2020-11-27 11:37

There are a few questions on the topic but none of them seem to cover my case, thus I\'m creating a new one.

I have JSON like the following:

{\"foo\"         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 11:50

    I was working on something like this. But is working only with structures generated from proto. https://github.com/flowup-labs/grpc-utils

    in your proto

    message Msg {
      Firstname string = 1 [(gogoproto.jsontag) = "name.firstname"];
      PseudoFirstname string = 2 [(gogoproto.jsontag) = "lastname"];
      EmbedMsg = 3  [(gogoproto.nullable) = false, (gogoproto.embed) = true];
      Lastname string = 4 [(gogoproto.jsontag) = "name.lastname"];
      Inside string  = 5 [(gogoproto.jsontag) = "name.inside.a.b.c"];
    }
    
    message EmbedMsg{
       Opt1 string = 1 [(gogoproto.jsontag) = "opt1"];
    }
    

    Then your output will be

    {
    "lastname": "Three",
    "name": {
        "firstname": "One",
        "inside": {
            "a": {
                "b": {
                    "c": "goo"
                }
            }
        },
        "lastname": "Two"
    },
    "opt1": "var"
    }
    

提交回复
热议问题