Reputation: 1464
How do I get this to work?
s, _ := url.QueryUnescape("%22%7B%5C%22sessionId%5C%22%3A%5C%225331b937-7b55-4c2d-798a-25e574a7e8af%5C%22%2C%5C%22userId%5C%22%3A2%2C%5C%22userName%5C%22%3A%5C%22datami_op%5C%22%2C%5C%22userEmail%5C%22%3A%5C%22datami_op%40example.com%5C%22%2C%5C%22userRoles%5C%22%3A%5B%5C%22operator%5C%22%5D%7D%22")
fmt.Println(s)
//s := "{\"sessionId\":\"5331b937-7b55-4c2d-798a-25e574a7e8af\",\"userId\":2,\"userName\":\"op\",\"userEmail\":\"[email protected]\",\"userRoles\":[\"operator\"]}"
var i Info
err := json.Unmarshal([]byte(s), &i)
fmt.Println(i, err)
Upvotes: 1
Views: 2667
Reputation: 131968
I believe this does what you want: GoPlay
Essentially you implement unmarshalJsonJson
(clever name, I know)...
The function will unmarshal as a json string, then use that string in the Info unmarshalling.
func unmarshalJsonJson(inval string) (*Info, error) {
var s string
err := json.Unmarshal([]byte(inval), &s)
if err != nil {
return nil, err
}
info := new(Info)
err = json.Unmarshal([]byte(s), info)
if err != nil {
return nil, err
}
return info, nil
}
OUTPUT
main.Info{
SessionId:"5331b937-7b55-4c2d-798a-25e574a7e8af",
UserId:2,
Username:"datami_op",
Email:"[email protected]",
Roles:[]string{"operator"},
}
Upvotes: 1
Reputation: 109326
You can either manually remove the quoting yourself, as you have in your comment, or you could unmarshal first as a json string:
var unquote string
err := json.Unmarshal([]byte(s), &unquote)
fmt.Println(unquote, err)
var i Info
err = json.Unmarshal([]byte(unquote), &i)
fmt.Println(i, err)
Upvotes: 1