multidimensional array - Nested json unmarshaling with 2d slices into struct not working in golang -


i have json structure looks like

{ "devices": [     {         "server": {             "bu": {                 "add_info": false,                 "applications": [                     [                         "systems",                         12                     ],                     [                         "someproject",                         106                     ]                 ],                 "name": [                     [                         "somename",                         4                     ],                     [                         "commonname",                         57                     ]                 ],                 "owners": [                     "someowner1",                     "someowner2"                 ],                 "users": [                     "someuser1",                     "someuser2"                 ]             }         }     }   ] } 

i trying add struct, , struct looks like

type twod [][]string type maincontainer struct {     devices []struct{         server struct{             bu struct{                 add_info string `json:"add_info"`                 applications twod `json:"applications"`                 name twod `json:"name"`                 owners []string `json:"owners"`                 users []string `json:"users"`                } `json:"bu"`                } `json:"server"`     } `json:"devices"` } 

however, when printing struct, 1 value 2d slice , nothing beyond it.

func main() { jsonfile, err  := ioutil.readfile("./search.json") if err != nil {     fmt.println(err)     os.exit(1) } var jsonobject maincontainer json.unmarshal(jsonfile, &jsonobject) fmt.printf("%v", jsonobject) } 

{[{{{ [[systems ]] [] [] []}}}]}

but if omitting 2d slices in struct like

type maincontainer struct { devices []struct{         server struct{             bu struct{                 add_info string `json:"add_info"`                 //applications twod `json:"applications"`                 //name twod `json:"name"`                 owners []string `json:"owners"`                 users []string `json:"users"`                } `json:"bu"`                } `json:"server"`     } `json:"devices"` } 

everything printed as

{[{{{ [someowner1 someowner2] [someuser1 someuser2]}}}]}

could me identify wrong here?

here link golang playground struct , sample json. 2 twod slices commented in struct there.

note:: edited playground link 1 2d slice uncommented difference can noted, , changed type string bool, pointed out @cnicutar, thank you.

the main problem you're not handling error returned json.unmarshal. once handle that, problems json (and decoding struct) become obvious.

if err := json.unmarshal(jsonfile, &jsonobject); err != nil {     fmt.printf("unmarshal: %v\n", err) } 

first:

unmarshal json: cannot unmarshal bool go value of type string

so add_info should bool. after fixing , uncommenting applications:

unmarshal json: cannot unmarshal number go value of type string

after changing 12 "12" , 106 "106" result is:

{[{{{false [[systems 12] [someproject 106]] [someuser1 someuser2]}}}]} 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -