go - Umarshalling this JSON in Golang -


i'm teaching myself how use json package in golang. seems straightforward lot of things, i'm having troubles parsing json data retrieved 3d printer. json looks this:

{     "tasks": [         {             "class": "task",             "id": "5fee231a",             "instances": {                 "28253266": {                     "class": "stateinstance",                     "id": "28253266",                     "progress": 1,                     "statetype": "y-edgeavoiding"                 },                 "1d774b49": {                     "class": "stateinstance",                     "id": "1d774b49",                     "progress": 1,                     "statetype": "x-calibration"                 },             },             "statetype": "startingup"         }     ] } 

(nb: there's few more "instances", didn't include them brevity, follow pattern, different statetype)

basically, printer has task doing (in example above, task has id of 5fee231a) , within it, sub-tasks (e.g. task 28253266).

if use code:

    var vals interface{}     err = json.unmarshal([]byte(myjson), &vals)      if err != nil {         fmt.println("error:", err)     }      spew.dump(&vals) 

(using github.com/davecgh/go-spew dump variable), output (nb: isn't whole output, it's snipped brevity :)):

(*map[string]interface {})(0xc0820068e0)((len=1) {  (string) (len=5) "tasks": ([]interface {}) (len=1 cap=1) {   (map[string]interface {}) (len=4) {    (string) (len=5) "class": (string) (len=4) "task",    (string) (len=2) "id": (string) (len=8) "5fee231a",    (string) (len=9) "instances": (map[string]interface {}) (len=13) {     (string) (len=8) "bd65d028": (map[string]interface {}) (len=4) {      (string) (len=5) "class": (string) (len=13) "stateinstance",      (string) (len=2) "id": (string) (len=8) "bd65d028",      (string) (len=8) "progress": (float64) 1,      (string) (len=9) "statetype": (string) (len=17) "centeringposition"     },     (string) (len=8) "d1e225e7": (map[string]interface {}) (len=4) {      (string) (len=2) "id": (string) (len=8) "d1e225e7",      (string) (len=8) "progress": (float64) 1,      (string) (len=9) "statetype": (string) (len=10) "targetcold",      (string) (len=5) "class": (string) (len=13) "stateinstance"     }, 

this nice, i'd be able grab status of given instance (e.g. ask progress of x-calibration, 1 in return). created structs:

type message struct {     tasks []struct {         class     string         id        string         instances []struct {             []struct {                 class     string                 id        string                 statetype string             }         }     } } 

tasks gets unmarshalled, none of instances:

====================== error: json: cannot unmarshal object go value of type []struct { struct { class string; id string; statetype string } } (*buccaneer.message)(0xc082002c40)({  tasks: ([]struct { class string; id string; instances []struct { struct { class string; id string; statetype string } } }) (len=1 cap=4) {   (struct { class string; id string; instances []struct { struct { class string; id string; statetype string } } }) {    class: (string) (len=4) "task",    id: (string) (len=8) "5fee231a",    instances: ([]struct { struct { class string; id string; statetype string } }) <nil>   }  } }) 

and note something struct in there. don't know name struct, instances have unique names (i.e. each "sub-task" has different id)

any appreciated :)

edit: updated show unmarshalling partially working, except instances

  • instances needs map, because it's object.

  • you don't need something inside of it, since it's map[string]struct{...}.

  • (also id should be id, that's stylistic nitpick.)

the type:

type message struct {     tasks []struct {         class     string         id        string         instances map[string]struct {             class     string             id        string             statetype string         }     } } 

playground: http://play.golang.org/p/r-wjaeiwp0.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -