1 回答
TA贡献1752条经验 获得超4个赞
使用原型图:
message Test {
map<string, Service> services = 1;
}
message Service {
string command = 1;
string root = 2;
}
proto map 是在 Go中编译的,因此在这种情况下,这是使用任意键对 JSON 建模的推荐方法。map[K]Vmap[string]*Service
这将给出以下输出:
services:{key:"service1" value:{command:"command1" root:"/"}} services:{key:"service2" value:{command:"command2" root:"/"}}
示例程序:
package main
import (
"encoding/json"
"example.com/pb"
"fmt"
)
const file = `{
"services": {
"service1": {
"command": "command1",
"root": "/"
},
"service2": {
"command": "command2",
"root": "/"
}
}
}
`
func main() {
test := &pb.Test{}
err := json.Unmarshal([]byte(file), test)
if err != nil {
panic(err)
}
fmt.Println(test)
}
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报