your json and your struct are not representing the same object
your struct should be as follows:
type AccessControl struct {
SessionID string `json:"sessionId"`
Door string `json:"door"`
Access bool `json:"access"`
}
and for the "full example" that you asked for:
type ServerAccessControl struct {
SessionID string `json:"sessionId"`
Door string `json:"door"`
Access bool `json:"access"`
}
type AccessControl struct {
SessionId string `json:"sessionId"`
DoorAccess map[string]bool
}
func main() {
jsn := `{ "sessionId": "232", "door": "Main Door", "access": true }`
var data ServerAccessControl
json.Unmarshal([]byte(jsn), &data)
accessControl := ServerAccessControlToAccessControl(&data)
fmt.Println(accessControl)
}
//ServerAccessControlToAccessControl convert a access control obj from the server into a map based struct.
func ServerAccessControlToAccessControl(fromServer *ServerAccessControl) AccessControl {
var accessControl AccessControl
accessControl.SessionId = fromServer.SessionID
accessControl.DoorAccess = make(map[string]bool)
accessControl.DoorAccess[fromServer.Door] = fromServer.Access
return accessControl
}
I created the ServerAccessControl struct which represent the json that you are getting from the server and the Access Control class as you wanted it.
It is important that you will notice that you are not getting a collection from the server, and if you plan to add more doors into your map, you will need to implement the logic your self.