0

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 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I wasn't sure how to check and handle that unmarshal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.