遇到将文本解组为结构体时出现问题。

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

Trouble unmarshaling text into struct

问题

尝试将一个 JSON 文本解组为自定义的结构体。我的结构体定义似乎是正确的,但 json.Unmarshal 没有返回任何内容。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type CmdUnit struct {
  7. Command string
  8. Description string
  9. }
  10. type CmdList struct {
  11. ListOfCommands []CmdUnit
  12. }
  13. type OneCmdList struct {
  14. Area string
  15. CmdList CmdList
  16. }
  17. type AllCommands struct {
  18. AllAreas []OneCmdList
  19. }
  20. func main() {
  21. jsonTxt := `
  22. {
  23. "Area1": [{
  24. "Command": "cmd1",
  25. "Desc": "cmd1 desc"
  26. }, {
  27. "Command": "cmd2",
  28. "Desc": "cmd2 desc"
  29. }],
  30. "Area2": [{
  31. "Command": "cmd1",
  32. "Desc": "cmd1 desc"
  33. }]
  34. }
  35. `
  36. cmds := AllCommands{}
  37. if err := json.Unmarshal([]byte(jsonTxt), &cmds); err != nil {
  38. fmt.Println("Failed to unmarshal:", err)
  39. } else {
  40. fmt.Printf("%+v\n", cmds)
  41. }
  42. }
  43. $ go run j.go
  44. {AllAreas:[]}

以上是你要翻译的内容。

英文:

Trying to unmarshal a json text into my own struct. My struct definitions seem correct but json.Unmarshal doesn't return anything.

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type CmdUnit struct {
  7. Command string
  8. Description string
  9. }
  10. type CmdList struct {
  11. ListOfCommands []CmdUnit
  12. }
  13. type OneCmdList struct {
  14. Area string
  15. CmdList CmdList
  16. }
  17. type AllCommands struct {
  18. AllAreas []OneCmdList
  19. }
  20. func main() {
  21. jsonTxt := `
  22. {
  23. "Area1": [{
  24. "Command": "cmd1",
  25. "Desc": "cmd1 desc"
  26. }, {
  27. "Command": "cmd2",
  28. "Desc": "cmd2 desc"
  29. }],
  30. "Area2": [{
  31. "Command": "cmd1",
  32. "Desc": "cmd1 desc"
  33. }]
  34. }
  35. `
  36. cmds := AllCommands{}
  37. if err := json.Unmarshal([]byte(jsonTxt), &cmds); err != nil {
  38. fmt.Println("Failed to unmarshal:", err)
  39. } else {
  40. fmt.Printf("%+v\n", cmds)
  41. }
  42. }
  43. $ go run j.go
  44. {AllAreas:[]}

答案1

得分: 3

你的结构体与你提供的 JSON 有不同的结构。在你的示例中,将结构体编组将导致以下 JSON 结构:

  1. {
  2. "AllAreas": [
  3. {
  4. "Area": "Area1",
  5. "CmdList": {
  6. "ListOfCommands": [
  7. {
  8. "Command": "cmd1",
  9. "Description": "cmd1 desc"
  10. },
  11. {
  12. "Command": "cmd2",
  13. "Description": "cmd2 desc"
  14. }
  15. ]
  16. }
  17. }
  18. ]
  19. }

在你的示例中,可以直接将 JSON 反编组为 map[string][]CmdUnit{},只需将 CmdUnit.Description 更改为 CmdUnit.Desc

  1. cmds := map[string][]CmdUnit{}
  2. if err := json.Unmarshal(jsonTxt, &cmds); err != nil {
  3. log.Fatal("Failed to unmarshal:", err)
  4. }
  5. fmt.Printf("%+v\n", cmds)

链接:https://play.golang.org/p/DFLYAfNLES

英文:

Your structs have a different structure from the json you're providing. Marshalling the structs in your example would result in json that looks like:

  1. {
  2. "AllAreas": [
  3. {
  4. "Area": "Area1",
  5. "CmdList": {
  6. "ListOfCommands": [
  7. {
  8. "Command": "cmd1",
  9. "Description": "cmd1 desc"
  10. },
  11. {
  12. "Command": "cmd2",
  13. "Description": "cmd2 desc"
  14. }
  15. ]
  16. }
  17. }
  18. ]
  19. }

The json in your example can be unmarshaled directly into a map[string][]CmdUnit{} with the minor change of CmdUnit.Description to CmdUnit.Desc.

  1. cmds := map[string][]CmdUnit{}
  2. if err := json.Unmarshal(jsonTxt, &cmds); err != nil {
  3. log.Fatal("Failed to unmarshal:", err)
  4. }
  5. fmt.Printf("%+v\n", cmds)

https://play.golang.org/p/DFLYAfNLES

huangapple
  • 本文由 发表于 2017年2月23日 07:48:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/42404484.html
匿名

发表评论

匿名网友

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

确定