解析没有键的多类型数据的 JSON。

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

Unmarshal json that has multile type data without key

问题

让我帮你翻译一下:

假设我有以下的 JSON 数据:

  1. {
  2. "a": [
  3. [
  4. "aaa",
  5. 15
  6. ],
  7. [
  8. "bbb",
  9. 11
  10. ]
  11. ]
  12. }

我有以下的代码:

  1. func main() {
  2. XJson := `
  3. {
  4. "a": [
  5. [
  6. "aaa",
  7. 15
  8. ],
  9. [
  10. "bbb",
  11. 11
  12. ]
  13. ]
  14. }`
  15. var Output StructJson
  16. json.Unmarshal([]byte(XJson), &Output)
  17. fmt.Println(Output)
  18. }
  19. type StructJson struct {
  20. [][]string `json:"a"`
  21. // 这里是什么?
  22. }

如何进行解组(unmarshal)呢?
我的意思是,"aaa" 和 "bbb" 是字符串类型的数据,而 15 和 11 是整数类型的数据。
如果我使用上述的 "错误" 结构体,输出结果是:

  1. {[[aaa ] [bbb ]]}

请问我应该在这里做什么?

英文:

Let say I have json like this:

  1. {
  2. "a": [
  3. [
  4. "aaa",
  5. 15
  6. ],
  7. [
  8. "bbb",
  9. 11
  10. ]
  11. ]
  12. }

I have this code :

  1. func main() {
  2. XJson := `
  3. {
  4. "a": [
  5. [
  6. "aaa",
  7. 15
  8. ],
  9. [
  10. "bbb",
  11. 11
  12. ]
  13. ]
  14. }`
  15. var Output StructJson
  16. json.Unmarshal([]byte(XJson), &Output)
  17. fmt.Println(Output)
  18. }
  19. type StructJson struct {
  20. [][]string `json:"a"` //wrong because have 15 and 11 have int type data
  21. ///what must i do in here?
  22. }

How to unmarshal that?
I mean, that "aaa" and "bbb" have type data string, 15 and 11 have type data integer.
If i use that "wrong" struct, output is

  1. {[[aaa ] [bbb ]]}

答案1

得分: -1

@ekomonimo 正如 @colm.anseo 提到的那样,为了解决这类问题,我们可以使用 interface{}

以下是如何使用它的示例代码:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. )
  7. func main() {
  8. XJson := `
  9. {
  10. "a": [
  11. [
  12. "aaa",
  13. 15
  14. ],
  15. [
  16. "bbb",
  17. 11
  18. ]
  19. ]
  20. }`
  21. var Output StructJson
  22. json.Unmarshal([]byte(XJson), &Output)
  23. fmt.Println("Output:", Output)
  24. // 输出:aaa string
  25. fmt.Println("Data on path Output.A[0][0]:", Output.A[0][0], reflect.TypeOf(Output.A[0][0]))
  26. // 输出:15 float64
  27. fmt.Println("Data on path Output.A[0][1]:", Output.A[0][1], reflect.TypeOf(Output.A[0][1]))
  28. }
  29. type StructJson struct {
  30. A [][]interface{} `json:"a"`
  31. }

希望对你有帮助!

英文:

@ekomonimo as @colm.anseo mentioned, for solving this sort of problems we can use the interface{}

Here how to use it:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. )
  7. func main() {
  8. XJson := `
  9. {
  10. "a": [
  11. [
  12. "aaa",
  13. 15
  14. ],
  15. [
  16. "bbb",
  17. 11
  18. ]
  19. ]
  20. }`
  21. var Output StructJson
  22. json.Unmarshal([]byte(XJson), &Output)
  23. fmt.Println("Output:", Output)
  24. // Prints: aaa string
  25. fmt.Println("Data on path Output.A[0][0]:", Output.A[0][0], reflect.TypeOf(Output.A[0][0]))
  26. // Prints: 15 float64
  27. fmt.Println("Data on path Output.A[0][1]:", Output.A[0][1], reflect.TypeOf(Output.A[0][1]))
  28. }
  29. type StructJson struct {
  30. A [][]interface{} `json:"a"`
  31. }

huangapple
  • 本文由 发表于 2021年11月5日 22:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/69854444.html
匿名

发表评论

匿名网友

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

确定