Golang:解析 JSON 对象导致空的映射。

huangapple go评论79阅读模式
英文:

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之后有一个无效字符{

playground示例

英文:

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.

playground example

huangapple
  • 本文由 发表于 2016年3月30日 12:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/36299902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定