【发布时间】:2018-05-06 02:36:57
【问题描述】:
我有这个 JSON 数据:
{
"InfoA" : [256,256,20000],
"InfoB" : [256,512,15000],
"InfoC" : [208,512,20000],
"DEFAULT" : [256,256,20000]
}
使用JSON-to-Go,我得到了这个 Go 类型定义:
type AutoGenerated struct {
InfoA []int `json:"InfoA"`
InfoB []int `json:"InfoB"`
InfoC []int `json:"InfoC"`
DEFAULT []int `json:"DEFAULT"`
}
使用此代码 (play.golang.org)
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
)
func main() {
type paramsInfo struct {
InfoA []int `json:"InfoA"`
InfoB []int `json:"InfoB"`
InfoC []int `json:"InfoC"`
DEFAULT []int `json:"DEFAULT"`
}
rawJSON := []byte(`{
"InfoA" : [256,256,20000],
"InfoB" : [256,512,15000],
"InfoC" : [208,512,20000],
"DEFAULT" : [256,256,20000]
}`)
var params []paramsInfo
err := json.Unmarshal(rawJSON, ¶ms)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
我收到错误json: cannot unmarshal object into Go value of type []main.paramsInfo
我不明白为什么。你能帮帮我吗?
【问题讨论】:
-
错误的哪一部分让您感到困惑?它可以准确地告诉您问题所在。