如何使用Go结构体表示这个复杂的数据结构?

huangapple go评论119阅读模式
英文:

How to represent this complex data structure with Go structs?

问题

所以我决定再给Go一次机会,但是遇到了困难。大部分Go结构体的示例在文档中都非常简单,我发现了以下的JSON对象表示法,但不知道如何用Go结构体表示:

  1. {
  2. id: 1,
  3. version: "1.0",
  4. method: "someString",
  5. params: [
  6. {
  7. clientid: "string",
  8. nickname: "string",
  9. level: "string"
  10. },
  11. [{
  12. value: "string",
  13. "function": "string"
  14. }]
  15. ]
  16. }

作为经验丰富的Go开发者,你会如何表示这种有点奇怪的数据?如何初始化结果结构体中的嵌套元素?

英文:

So I decided to give Go another chance but got stuck. Most Go struct examples in documentation are very simple and I found the following JSON object notation that I don't know how to represent with Go structs:

  1. {
  2. id: 1,
  3. version: "1.0",
  4. method: "someString",
  5. params: [
  6. {
  7. clientid: "string",
  8. nickname: "string",
  9. level: "string"
  10. },
  11. [{
  12. value: "string",
  13. "function": "string"
  14. }]
  15. ]
  16. }

How would you, more experienced gophers, represent that somewhat strange data in Go? And how to initialize the nested elements of the resulting struct?

答案1

得分: 12

我会使用json.RawMessage切片来表示params属性,然后通过一个GetXXX方法将其隐藏起来,以便进行良好的解码。类似于以下方式:

  1. type Outer struct {
  2. Id int `json:"id"`
  3. Version string `json:"version"`
  4. Method string `json:"method"`
  5. Params []json.RawMessage `json:"params"`
  6. }
  7. type Client struct {
  8. ClientId string `json:"clientid"`
  9. Nickname string `json:"nickname"`
  10. Level string `json:"level"`
  11. }
  12. ...
  13. obj := Outer{}
  14. err := json.Unmarshal([]byte(js), &obj)
  15. if err != nil {
  16. fmt.Println(err)
  17. }
  18. fmt.Println(obj.Method) // 输出 "someString"
  19. client := Client{}
  20. err = json.Unmarshal(obj.Params[0], &client)
  21. fmt.Println(client.Nickname) // 输出 "string"

这是一个快速拼凑的示例,第二个param需要你提供一些输入...但基本上你需要将其解码为一个表示该类型的切片。

英文:

I would use a json.RawMessage slice for the params property.. then hide them behind an GetXXX method that decodes it all nicely. Somewhat like this:

  1. type Outer struct {
  2. Id int `json:"id"`
  3. Version string `json:"version"`
  4. Method string `json:"method"`
  5. Params []json.RawMessage `json:"params"`
  6. }
  7. type Client struct {
  8. ClientId string `json:"clientid"`
  9. Nickname string `json:"nickname"`
  10. Level string `json:"level"`
  11. }
  12. ....
  13. obj := Outer{}
  14. err := json.Unmarshal([]byte(js), &obj)
  15. if err != nil {
  16. fmt.Println(err)
  17. }
  18. fmt.Println(obj.Method) // prints "someString"
  19. client := Client{}
  20. err = json.Unmarshal(obj.Params[0], &client)
  21. fmt.Println(client.Nickname) // prints "string"

Working (quickly smashed together at lunch time) sample: http://play.golang.org/p/Gp7UKj6pRK

That second param will need some input from you .. but you're basically looking at decoding it to a slice of whatever type you create to represent it.

huangapple
  • 本文由 发表于 2014年11月28日 11:22:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/27181424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定