Golang:Unmarshal JSON Object导致空映射

时间:2016-03-30 04:46:22

标签: json go

我一直试图解组一个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)
    }
}

有谁看到我做错了什么?感谢。

1 个答案:

答案 0 :(得分:4)

检查并处理从json.Unmarshal([]byte(buff), &s)返回的错误。您会发现在偏移量为1685的数组元素之后存在无效字符“{”。

playground example