the raw of response.body is this { "args": {}, "data": "{\"firstname\":\"Nic\",\"lastname\":\"Raboy\"}", "files": {}, "form": {}, "headers": { "Accept-Encoding": "gzip", "Connection": "close", "Content-Length": "38", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "Go-http-client/1.1" }, "json": { "firstname": "Nic", "lastname": "Raboy" }, "origin": "212.194.99.97", "url": "https://httpbin.org/post" }
for origin with or without the tag it works Le mardi 14 août 2018 13:14:40 UTC+2, Ian Davis a écrit : > > > On Tue, 14 Aug 2018, at 11:19 AM, nicolas_boiteux via golang-nuts wrote: > > Hello > > I need some assistance to decode a json content > > I tried to use only usefull code to do it but don't success to retrieve > the data part of the json result. > > > var user struct { > Firstname string `json: "firstname"` > Lastname string `json: "lastname"` > Origin string `"origin"` > } > > jsonData := map[string]string{"firstname": "Nic", "lastname": "Raboy"} > jsonValue, _ := json.Marshal(jsonData) > response, err := http.Post("https://httpbin.org/post", > "application/json", bytes.NewBuffer(jsonValue)) > defer response.Body.Close() > json.NewDecoder(response.Body).Decode(&user) > fmt.Println(user) > > > but this code only map the origin field not the firstname, and lastname > part. > > > Please show an example of the JSON you are receiving. > > You have some errors in your struct. Remove the spaces between json: and > the quote, like this: `json:"firstname"` > > Also your origin tag is missing the json prefix. > > > Ian > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.