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

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

Unmarshal json that has multile type data without key

问题

让我帮你翻译一下:

假设我有以下的 JSON 数据:

{
  "a": [
    [
      "aaa",
      15
    ],
    [
      "bbb",
      11
    ]
  ]
}

我有以下的代码:

func main() {
	XJson := `
	{
	  "a": [
			[
			"aaa",
			15
			],
			[
			"bbb",
			11
			]
		]
	}`

	var Output StructJson

	json.Unmarshal([]byte(XJson), &Output)

	fmt.Println(Output)
}

type StructJson struct {
	[][]string `json:"a"`
    // 这里是什么?
}

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

{[[aaa ] [bbb ]]}

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

英文:

Let say I have json like this:

{
	  "a": [
			[
			"aaa",
			15
			],
			[
			"bbb",
			11
			]
		]
	}

I have this code :

func main() {
	XJson := `
	{
	  "a": [
			[
			"aaa",
			15
			],
			[
			"bbb",
			11
			]
		]
	}`

	var Output StructJson

	json.Unmarshal([]byte(XJson), &Output)

	fmt.Println(Output)
}

type StructJson struct {
	[][]string `json:"a"`  //wrong because have 15 and 11 have int type data
    ///what must i do in here?
}



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

{[[aaa ] [bbb ]]}

答案1

得分: -1

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

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

package main

import (
	"encoding/json"
	"fmt"
	"reflect"
)

func main() {
	XJson := `
    {
      "a": [
            [
            "aaa",
            15
            ],
            [
            "bbb",
            11
            ]
        ]
    }`

	var Output StructJson

	json.Unmarshal([]byte(XJson), &Output)

	fmt.Println("Output:", Output)
	// 输出:aaa string
	fmt.Println("Data on path Output.A[0][0]:", Output.A[0][0], reflect.TypeOf(Output.A[0][0]))
	// 输出:15 float64
	fmt.Println("Data on path Output.A[0][1]:", Output.A[0][1], reflect.TypeOf(Output.A[0][1]))
}

type StructJson struct {
	A [][]interface{} `json:"a"`
}

希望对你有帮助!

英文:

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

Here how to use it:

package main

import (
	"encoding/json"
	"fmt"
	"reflect"
)

func main() {
	XJson := `
    {
      "a": [
            [
            "aaa",
            15
            ],
            [
            "bbb",
            11
            ]
        ]
    }`

	var Output StructJson

	json.Unmarshal([]byte(XJson), &Output)

	fmt.Println("Output:", Output)
	// Prints: aaa string
	fmt.Println("Data on path Output.A[0][0]:", Output.A[0][0], reflect.TypeOf(Output.A[0][0]))
	// Prints: 15 float64
	fmt.Println("Data on path Output.A[0][1]:", Output.A[0][1], reflect.TypeOf(Output.A[0][1]))
}

type StructJson struct {
	A [][]interface{} `json:"a"`
}

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:

确定