[英]Golang: Unmarshal JSON Object results in empty map
我一直在試圖解組json對象。 我沒有收到任何錯誤,但之后卻得到了一個空的地圖。 我是新手,所以我仍在努力圍繞某些語言構造。
這是我的數據源的樣子
{
"animationStates": {
"idle": {
"numFrames": 4,
"frames": [
{
"frame": {"x":43,"y":106,"w":12,"h":15},
"rotated": false
},
{
"frame": {"x":1,"y":143,"w":12,"h":14},
"rotated": false
},
{
"frame": {"x":71,"y":132,"w":12,"h":14},
"rotated": false
},
{
"frame": {"x":15,"y":126,"w":12,"h":15},
"rotated": true
}
]
},
"run": {
"numFrames": 10,
"frames": [
{
"frame": {"x":73,"y":82,"w":12,"h":15},
"rotated": false
},
{
"frame": {"x":29,"y":125,"w":12,"h":15},
"rotated": false
},
{
"frame": {"x":33,"y":36,"w":12,"h":16},
"rotated": false
},
{
"frame": {"x":1,"y":107,"w":12,"h":16},
"rotated": false
},
{
"frame": {"x":1,"y":89,"w":12,"h":16},
"rotated": false
},
{
"frame": {"x":17,"y":54,"w":12,"h":16},
"rotated": false
},
{
"frame": {"x":1,"y":125,"w":12,"h":16},
"rotated": false
}
{
"frame": {"x":15,"y":143,"w":12,"h":14},
"rotated": false
},
{
"frame": {"x":29,"y":142,"w":12,"h":14},
"rotated": false
},
{
"frame": {"x":72,"y":99,"w":12,"h":15},
"rotated": false
}
]
},
"roll": {
"numFrames": 5,
"frames": [
{
"frame": {"x":71,"y":116,"w":12,"h":14},
"rotated": false
},
{
"frame": {"x":71,"y":148,"w":12,"h":10},
"rotated": false
},
{
"frame": {"x":73,"y":47,"w":12,"h":15},
"rotated": false
},
{
"frame": {"x":57,"y":147,"w":12,"h":11},
"rotated": false
},
{
"frame": {"x":29,"y":108,"w":12,"h":15},
"rotated": false
}
]
},
"jump": {
"numFrames": 1,
"frames": [
{
"frame": {"x":45,"y":54,"w":12,"h":16},
"rotated": false
}
]
},
"fall": {
"numFrames": 1,
"frames": [
{
"frame": {"x":29,"y":90,"w":12,"h":16},
"rotated": false
}
]
},
"shoot": {
"numFrames": 5,
"frames": [
{
"frame": {"x":37,"y":1,"w":14,"h":15},
"rotated": false
},
{
"frame": {"x":17,"y":37,"w":14,"h":15},
"rotated": false
},
{
"frame": {"x":1,"y":37,"w":14,"h":16},
"rotated": false
},
{
"frame": {"x":19,"y":19,"w":14,"h":15},
"rotated": false
},
{
"frame": {"x":1,"y":55,"w":14,"h":15},
"rotated": false
}
]
}
}
}
這是我用來解組的代碼
package main
import (
"encoding/json"
"io/ioutil"
)
type AnimationFrame struct {
Frame struct {
X int `json:"x"`
Y int `json:"y"`
W int `json:"w"`
H int `json:"h"`
} `json:"frame"`
Rotated bool `json:"rotated"`
}
type Animation struct {
NumFrames int `json:"numFrames"`
Frames []AnimationFrame `json:"frames"`
}
type StateList struct {
Actions map[string]Animation `json:"animationStates"`
}
func NewAnimation (file string) *StateList {
list := make(map[string]Animation)
s := &StateList{list}
buff, err := ioutil.ReadFile(file)
check(err)
json.Unmarshal([]byte(buff), &s)
return s
}
func check (e error) {
if e != nil {
panic(e)
}
}
有人看到我在做什么錯嗎? 謝謝。
檢查並處理從json.Unmarshal([]byte(buff), &s)
返回的錯誤。 您會發現數組元素后的偏移量1685處有一個無效字符'{'。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.