英文:
Golang: Unmarshal JSON Object results in empty map
问题
我一直在尝试解析一个 JSON 对象,但是我没有得到任何错误,但是之后得到了一个空的映射。我对 Go 还不熟悉,所以我仍然在努力理解一些语言结构。
这是我的数据源的样子:
{
"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)
}
}
有人看到我做错了什么吗?谢谢。
英文:
I've been stuck trying to unmarshal a json object. I am not getting any errors but I am getting an empty map afterwards. I'm new to go so I'm still trying to wrap my head around some of the language constructs.
Here is what my data source looks like
{
"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
}
]
}
}
}
and here is the code that I'm using to unmarshal
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)
}
}
Does anyone see what I'm doing wrong? Thanks.
答案1
得分: 3
检查并处理从json.Unmarshal([]byte(buff), &s)
返回的错误。你会发现在偏移量1685之后有一个无效字符{
。
英文:
Check and handle the error returned from json.Unmarshal([]byte(buff), &s)
. You will find that there's an invalid character '{' after array element at offset 1685.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论