有这个json文件:
{
"colors": [
["#7ad9ab", "#5ebd90", "#41a277", "#21875e", "#713517"],
["#5ebd90", "#41a277", "#21875e", "#006d46", "#561e00"],
["#005430"]
]
}
这段代码:
type Palette struct {
Colors []string
}
func TestStuff(t *testing.T) {
c, err := os.Open("palette.json")
if err != nil {
fmt.Printf("Error: %v", err.Error())
}
defer c.Close()
bc, _ := ioutil.ReadAll(c)
var palette []Palette //also tried with Palette
err = json.Unmarshal(bc, &palette)
if err != nil {
fmt.Printf("Error: %v \n", err.Error())
}
fmt.Printf("Data: %v", palette)
}
并不断得到:
错误:json:无法将数组解组到Go结构字段Palette中。字符串类型的颜色
或类似,如果我更改调色板类型。尖端?谢谢!
相关分类