Golang JSON如何解码具有不同元素数据类型的二维数组

I need to parse json data, and I have no problem parsing object structures or pure arrays (of the same type of elements)

go version go1.12.9 darwin/amd64

Json format for ([uint8,uint8,uint32,uint16,uint16,[3][20]int16][])

[
    [
        0,
        0,
        297,
        153,
        78,
        [
            [1871, 1547, ...],
            [...],
            [...]
        ]
    ]
    ...
]

Needs to be resolved to the following structure

type HeartBeat struct {
    Template uint8
    Calssify uint8
    Index    uint32
    Tr       uint16
    Hr       uint16
    Feature  [3][20]int16
}

How to parse

One way you can do is:

decoder:=json.NewDecoder(input)
decoder.UseNumber()
var array []interface{}
decoder.Decode(&array)
heartBeat.Template=uint8(array[0].(json.Number).Int64())
heartBeat.Index=uint32(array[2].(json.Number).Int64())
...
feature:=array[5].([]interface{})
heartBeat.Feature[0][0]=int16(feature[0].([]interface{})[0].(json.Number).Int64())

Of course, you have to add error and bounds checking to the code.

I've solved it

var bytesData = []byte(`[........]`)
var entries [][]interface{}
if err := json.Unmarshal(bytesData, &entries); err != nil {
    log.Fatal(err)
}

for _, value := range entries {
    hb := HeartBeat{
        Template: uint8(value[0].(float64)),
        Calssify: uint8(value[1].(float64)),
        Index:    uint32(value[2].(float64)),
        Tr:       uint16(value[3].(float64)),
        Hr:       uint16(value[4].(float64)),
    }
    for arrIndex, arr := range value[5].([]interface{}) {
        for listIndex, list := range arr.([]interface{}) {
            hb.Feature[arrIndex][listIndex] = int16(list.(float64))
        }
    }
    fmt.Println(hb)
}

Online preview address play golang