无法将数字解组为Go值。

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

Unable to unmarshal number into Go Value

问题

我尝试避免问一些简单答案的问题,但我似乎无法弄清楚问题出在哪里...(标题中的问题)

相关代码:

  1. match := new(Match)
  2. if _, msgB, err = ws.ReadMessage(); err != nil {
  3. panic(err)
  4. } else {
  5. println(string(msgB))
  6. err = json.Unmarshal(msgB, match)
  7. if err != nil { panic(err) }
  8. }
  9. type Match struct {
  10. Teams [][]Char
  11. Map [][]Tile
  12. ID string //uuid
  13. Socket *websocket.Conn `json:'-'`
  14. }
  15. type Char struct {
  16. ID int
  17. HP int
  18. CT int
  19. Stats statList
  20. X int
  21. Y int
  22. ACList Actions
  23. }
  24. type statList struct {
  25. Str int
  26. Vit int
  27. Int int
  28. Wis int
  29. Dex int
  30. Spd int
  31. }
  32. type Actions struct {
  33. Actions []Action
  34. TICKCT int
  35. }

要解组的字符串(为了可见性进行格式化):

  1. {
  2. "Teams": [
  3. [
  4. {
  5. "ID": 1,
  6. "HP": 10,
  7. "CT": 0,
  8. "Stats": [
  9. 1,
  10. 1,
  11. 1,
  12. 1,
  13. 1,
  14. 1
  15. ],
  16. "X": 0,
  17. "Y": 0,
  18. "ACList": {
  19. "Actions": [],
  20. "TICKCT": 0
  21. }
  22. }
  23. ],
  24. [
  25. {
  26. "ID": 2,
  27. "HP": 10,
  28. "CT": 0,
  29. "Stats": [
  30. 1,
  31. 1,
  32. 1,
  33. 1,
  34. 1,
  35. 1
  36. ],
  37. "X": 2,
  38. "Y": 2,
  39. "ACList": {
  40. "Actions": [],
  41. "TICKCT": 0
  42. }
  43. }
  44. ]
  45. ],
  46. "Map": [
  47. [
  48. {
  49. "Depth": 1,
  50. "Type": 1,
  51. "Unit": 1
  52. },
  53. {
  54. "Depth": 1,
  55. "Type": 1,
  56. "Unit": null
  57. },
  58. {
  59. "Depth": 1,
  60. "Type": 1,
  61. "Unit": null
  62. }
  63. ],
  64. [
  65. {
  66. "Depth": 1,
  67. "Type": 1,
  68. "Unit": null
  69. },
  70. {
  71. "Depth": 1,
  72. "Type": 1,
  73. "Unit": null
  74. },
  75. {
  76. "Depth": 1,
  77. "Type": 1,
  78. "Unit": null
  79. }
  80. ],
  81. [
  82. {
  83. "Depth": 1,
  84. "Type": 1,
  85. "Unit": null
  86. },
  87. {
  88. "Depth": 1,
  89. "Type": 1,
  90. "Unit": null
  91. },
  92. {
  93. "Depth": 1,
  94. "Type": 1,
  95. "Unit": 2
  96. }
  97. ]
  98. ],
  99. "ID": "0b055e19-9b96-e492-b816-43297f12cc39"
  100. }

错误:

2014/03/28 12:11:41 http: panic serving 127.0.0.1:56436: json: cannot unmarshal number into Go value of type main.Char

英文:

I try to refrain from asking questions with simple answers but I can't seem to figure out what the issue is here... (Issue in title)

Relevant code:

  1. match := new(Match)
  2. if _, msgB, err = ws.ReadMessage(); err != nil {
  3. panic(err)
  4. }else {
  5. println(string(msgB))
  6. err = json.Unmarshal(msgB, match)
  7. if err != nil { panic(err) }
  8. }
  9. type Match struct {
  10. Teams [][]Char
  11. Map [][]Tile
  12. ID string //uuid
  13. Socket *websocket.Conn `json:'-'`
  14. }
  15. type Char struct {
  16. ID int
  17. HP int
  18. CT int
  19. Stats statList
  20. X int
  21. Y int
  22. ACList Actions
  23. }
  24. type statList struct {
  25. Str int
  26. Vit int
  27. Int int
  28. Wis int
  29. Dex int
  30. Spd int
  31. }
  32. type Actions struct {
  33. Actions []Action
  34. TICKCT int
  35. }

String to unmarshal (Formatted for visibility):

  1. {
  2. "Teams": [
  3. [
  4. {
  5. "ID": 1,
  6. "HP": 10,
  7. "CT": 0,
  8. "Stats": [
  9. 1,
  10. 1,
  11. 1,
  12. 1,
  13. 1,
  14. 1
  15. ],
  16. "X": 0,
  17. "Y": 0,
  18. "ACList": {
  19. "Actions": [],
  20. "TICKCT": 0
  21. }
  22. }
  23. ],
  24. [
  25. {
  26. "ID": 2,
  27. "HP": 10,
  28. "CT": 0,
  29. "Stats": [
  30. 1,
  31. 1,
  32. 1,
  33. 1,
  34. 1,
  35. 1
  36. ],
  37. "X": 2,
  38. "Y": 2,
  39. "ACList": {
  40. "Actions": [],
  41. "TICKCT": 0
  42. }
  43. }
  44. ]
  45. ],
  46. "Map": [
  47. [
  48. {
  49. "Depth": 1,
  50. "Type": 1,
  51. "Unit": 1
  52. },
  53. {
  54. "Depth": 1,
  55. "Type": 1,
  56. "Unit": null
  57. },
  58. {
  59. "Depth": 1,
  60. "Type": 1,
  61. "Unit": null
  62. }
  63. ],
  64. [
  65. {
  66. "Depth": 1,
  67. "Type": 1,
  68. "Unit": null
  69. },
  70. {
  71. "Depth": 1,
  72. "Type": 1,
  73. "Unit": null
  74. },
  75. {
  76. "Depth": 1,
  77. "Type": 1,
  78. "Unit": null
  79. }
  80. ],
  81. [
  82. {
  83. "Depth": 1,
  84. "Type": 1,
  85. "Unit": null
  86. },
  87. {
  88. "Depth": 1,
  89. "Type": 1,
  90. "Unit": null
  91. },
  92. {
  93. "Depth": 1,
  94. "Type": 1,
  95. "Unit": 2
  96. }
  97. ]
  98. ],
  99. "ID": "0b055e19-9b96-e492-b816-43297f12cc39"}

Error:

> 2014/03/28 12:11:41 http: panic serving 127.0.0.1:56436: json: cannot
> unmarshal number into Go value of type main.Char

答案1

得分: 1

我修改了代码的固定版本(playground)。这似乎是主要的错误:

  1. type Char struct {
  2. ID int
  3. HP int
  4. CT int
  5. Stats []int // 这里是 statList,这样是行不通的
  6. X int
  7. Y int
  8. ACList Actions
  9. }

另外请注意我对 Tile 的定义,允许数字为 nil

  1. type Tile struct {
  2. Depth int
  3. Type int
  4. Unit *int
  5. }

你没有提供所有的结构,所以我自己编写了一些——可能是错误的!总的来说,代码如下:

  1. import (
  2. "encoding/json"
  3. "fmt"
  4. )
  5. type Match struct {
  6. Teams [][]Char
  7. Map [][]Tile
  8. ID string //uuid
  9. // Socket *websocket.Conn `json:'-'`
  10. }
  11. type Char struct {
  12. ID int
  13. HP int
  14. CT int
  15. Stats []int // 这里是 statList,这样是行不通的
  16. X int
  17. Y int
  18. ACList Actions
  19. }
  20. type statList struct {
  21. Str int
  22. Vit int
  23. Int int
  24. Wis int
  25. Dex int
  26. Spd int
  27. }
  28. type Action string
  29. type Actions struct {
  30. Actions []Action
  31. TICKCT int
  32. }
  33. type Tile struct {
  34. Depth int
  35. Type int
  36. Unit *int
  37. }
  38. var data = `{
  39. "Teams": [
  40. [
  41. {
  42. "ID": 1,
  43. "HP": 10,
  44. "CT": 0,
  45. "Stats": [
  46. 1,
  47. 1,
  48. 1,
  49. 1,
  50. 1,
  51. 1
  52. ],
  53. "X": 0,
  54. "Y": 0,
  55. "ACList": {
  56. "Actions": [],
  57. "TICKCT": 0
  58. }
  59. }
  60. ],
  61. [
  62. {
  63. "ID": 2,
  64. "HP": 10,
  65. "CT": 0,
  66. "Stats": [
  67. 1,
  68. 1,
  69. 1,
  70. 1,
  71. 1,
  72. 1
  73. ],
  74. "X": 2,
  75. "Y": 2,
  76. "ACList": {
  77. "Actions": [],
  78. "TICKCT": 0
  79. }
  80. }
  81. ]
  82. ],
  83. "Map": [
  84. [
  85. {
  86. "Depth": 1,
  87. "Type": 1,
  88. "Unit": 1
  89. },
  90. {
  91. "Depth": 1,
  92. "Type": 1,
  93. "Unit": null
  94. },
  95. {
  96. "Depth": 1,
  97. "Type": 1,
  98. "Unit": null
  99. }
  100. ],
  101. [
  102. {
  103. "Depth": 1,
  104. "Type": 1,
  105. "Unit": null
  106. },
  107. {
  108. "Depth": 1,
  109. "Type": 1,
  110. "Unit": null
  111. },
  112. {
  113. "Depth": 1,
  114. "Type": 1,
  115. "Unit": null
  116. }
  117. ],
  118. [
  119. {
  120. "Depth": 1,
  121. "Type": 1,
  122. "Unit": null
  123. },
  124. {
  125. "Depth": 1,
  126. "Type": 1,
  127. "Unit": null
  128. },
  129. {
  130. "Depth": 1,
  131. "Type": 1,
  132. "Unit": 2
  133. }
  134. ]
  135. ],
  136. "ID": "0b055e19-9b96-e492-b816-43297f12cc39"}`
  137. func main() {
  138. match := new(Match)
  139. err := json.Unmarshal([]byte(data), match)
  140. if err != nil {
  141. panic(err)
  142. }
  143. fmt.Printf("match = %#v\n", match)
  144. }
英文:

I made a fixed version of the code (playground). This seemed to be the main mistake:

  1. type Char struct {
  2. ID int
  3. HP int
  4. CT int
  5. Stats []int // This was statList which won't work
  6. X int
  7. Y int
  8. ACList Actions
  9. }

Also note the definition I made of Tile which allows numbers to be nil.

  1. type Tile struct {
  2. Depth int
  3. Type int
  4. Unit *int
  5. }

You didn't provide all the structures so I made some up - probably wrong! All together that is:

  1. import (
  2. "encoding/json"
  3. "fmt"
  4. )
  5. type Match struct {
  6. Teams [][]Char
  7. Map [][]Tile
  8. ID string //uuid
  9. // Socket *websocket.Conn `json:'-'`
  10. }
  11. type Char struct {
  12. ID int
  13. HP int
  14. CT int
  15. Stats []int // This was statList which won't work
  16. X int
  17. Y int
  18. ACList Actions
  19. }
  20. type statList struct {
  21. Str int
  22. Vit int
  23. Int int
  24. Wis int
  25. Dex int
  26. Spd int
  27. }
  28. type Action string
  29. type Actions struct {
  30. Actions []Action
  31. TICKCT int
  32. }
  33. type Tile struct {
  34. Depth int
  35. Type int
  36. Unit *int
  37. }
  38. var data = `{
  39. "Teams": [
  40. [
  41. {
  42. "ID": 1,
  43. "HP": 10,
  44. "CT": 0,
  45. "Stats": [
  46. 1,
  47. 1,
  48. 1,
  49. 1,
  50. 1,
  51. 1
  52. ],
  53. "X": 0,
  54. "Y": 0,
  55. "ACList": {
  56. "Actions": [],
  57. "TICKCT": 0
  58. }
  59. }
  60. ],
  61. [
  62. {
  63. "ID": 2,
  64. "HP": 10,
  65. "CT": 0,
  66. "Stats": [
  67. 1,
  68. 1,
  69. 1,
  70. 1,
  71. 1,
  72. 1
  73. ],
  74. "X": 2,
  75. "Y": 2,
  76. "ACList": {
  77. "Actions": [],
  78. "TICKCT": 0
  79. }
  80. }
  81. ]
  82. ],
  83. "Map": [
  84. [
  85. {
  86. "Depth": 1,
  87. "Type": 1,
  88. "Unit": 1
  89. },
  90. {
  91. "Depth": 1,
  92. "Type": 1,
  93. "Unit": null
  94. },
  95. {
  96. "Depth": 1,
  97. "Type": 1,
  98. "Unit": null
  99. }
  100. ],
  101. [
  102. {
  103. "Depth": 1,
  104. "Type": 1,
  105. "Unit": null
  106. },
  107. {
  108. "Depth": 1,
  109. "Type": 1,
  110. "Unit": null
  111. },
  112. {
  113. "Depth": 1,
  114. "Type": 1,
  115. "Unit": null
  116. }
  117. ],
  118. [
  119. {
  120. "Depth": 1,
  121. "Type": 1,
  122. "Unit": null
  123. },
  124. {
  125. "Depth": 1,
  126. "Type": 1,
  127. "Unit": null
  128. },
  129. {
  130. "Depth": 1,
  131. "Type": 1,
  132. "Unit": 2
  133. }
  134. ]
  135. ],
  136. "ID": "0b055e19-9b96-e492-b816-43297f12cc39"}`
  137. func main() {
  138. match := new(Match)
  139. err := json.Unmarshal([]byte(data), match)
  140. if err != nil {
  141. panic(err)
  142. }
  143. fmt.Printf("match = %#v\n", match)
  144. }

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

发表评论

匿名网友

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

确定