I have a string in my Go module which is the body of a HTTP response. it looks something like this:
bodyString = `{"firstname": "foo", "lastname": "bar", "username": "foobar"}`
I want to convert it to the following Go struct:
type Params struct {
FirstName string
LastName string
Username string
PasswordHash string
EmailAddress string
}
I attempt to do so using the following:
var jsonMap map[string]interface{}
json.Unmarshal([]byte(bodyString), &jsonMap)
paramsIn.FirstName = jsonMap["firstname"].(string)
paramsIn.LastName = jsonMap["lastname"].(string)
paramsIn.Username = jsonMap["username"].(string)
paramsIn.PasswordHash = jsonMap["passwordhash"].(string)
paramsIn.EmailAddress = jsonMap["emailaddress"].(string)
However the unmarshal fails to match the data in the string to the appropriate keys. i.e. what is stored in the jsonMap variable is only empty strings.
I clearly am doing something wrong and I haven't worked with json in Go very much. If any one can help or show me the correct way to unmarshal json data from a string that would be great, thanks.
Unmarshal?