如何将具有两种不同数据类型的JSON数组解析为Golang中的结构

时间:2022-10-11 12:15:40

I have JSON that looks like this:

我有这样的JSON:

{
    "a": [
          [
              "12.58861425",
              10.52046452
          ],
          [
              "12.58861426",
              4.1073
          ]
        ]
    "b": [
          [
              "12.58861425",
              10.52046452
          ],
          [
              "12.58861426",
              4.1073
          ]
        ]
    "c": "true"
    "d": 1234
}
        

I want to unmarshall this into a struct I created:

我想将其解组为我创建的结构:

type OrderBook struct {
	A [][2]float32 `json:"a"`
	B [][2]float32 `json:"b"`
        C string `json:"c"`
	D uint32 `json:"d"`
}

//Note this won't work because the JSON array contains a string and a float value pair rather than only floats.

Normally I would then convert this JSON into a struct in Golang as so:

通常我会将这个JSON转换为Golang中的结构,如下所示:

orders := new(OrderBook)
err = json.Unmarshal(JSON, &orders)

Since I can't define the type struct to match the JSON this won't work. Doing a bit of reading, I think Unmarshal-ing into an interface, and then using the interface data through type checking may be a solution.

由于我无法定义类型结构以匹配JSON,因此无效。做一些阅读,我认为解组到一个接口,然后通过类型检查使用接口数据可能是一个解决方案。

Am I going in the right direction with this?
A template showing how I might go about using the data once it's in the interface would really help.

我是否朝着正确的方向前进?一个模板显示了我在界面中使用数据后如何使用它真的会有所帮助。

1 个解决方案

#1


1  

Why not just leave out declaring the types of the A and B slices like so?

为什么不省略声明A和B切片的类型呢?

data := []byte(`{"a": [["12.58861425",10.52046452],["12.58861426",4.1073]],"b": [["12.58861425",10.52046452],["12.58861426",4.1073]],"c": "true","d": 1234}`)

type OrderBook struct {
    A [][2]interface{} `json:"a"`
    B [][2]interface{} `json:"b"`
    C string           `json:"c"`
    D uint32           `json:"d"`
}
orders := new(OrderBook)
err := json.Unmarshal(data, &orders)
if err != nil {
    fmt.Printf(err.Error())
} else {
    fmt.Printf("%s\n", orders.A[0][0].(string))     
    fmt.Printf("%f\n", orders.A[0][1].(float64))
}

There's an example in the playground: https://play.golang.org/p/0MUY-yOYII

在操场上有一个例子:https://play.golang.org/p/0MUY-yOYII

I must disagree with evanmcdonnal, there are certainly use cases where where rolling your own Unmarshaller makes sense, this I do not believe is one of them. It doesn't get much simpler than what is shown here.

我必须不同意evanmcdonnal,当然有一些使用案例,你自己的Unmarshaller滚动是有意义的,我不相信是其中之一。它没有比这里显示的更简单。

#1


1  

Why not just leave out declaring the types of the A and B slices like so?

为什么不省略声明A和B切片的类型呢?

data := []byte(`{"a": [["12.58861425",10.52046452],["12.58861426",4.1073]],"b": [["12.58861425",10.52046452],["12.58861426",4.1073]],"c": "true","d": 1234}`)

type OrderBook struct {
    A [][2]interface{} `json:"a"`
    B [][2]interface{} `json:"b"`
    C string           `json:"c"`
    D uint32           `json:"d"`
}
orders := new(OrderBook)
err := json.Unmarshal(data, &orders)
if err != nil {
    fmt.Printf(err.Error())
} else {
    fmt.Printf("%s\n", orders.A[0][0].(string))     
    fmt.Printf("%f\n", orders.A[0][1].(float64))
}

There's an example in the playground: https://play.golang.org/p/0MUY-yOYII

在操场上有一个例子:https://play.golang.org/p/0MUY-yOYII

I must disagree with evanmcdonnal, there are certainly use cases where where rolling your own Unmarshaller makes sense, this I do not believe is one of them. It doesn't get much simpler than what is shown here.

我必须不同意evanmcdonnal,当然有一些使用案例,你自己的Unmarshaller滚动是有意义的,我不相信是其中之一。它没有比这里显示的更简单。