go - Handle response from post request to Json -
i'm using following code response server after post request:
type responsefrompost struct { n_expediente string enviar string } func main(){ ...... res, err := client.do(req) if err != nil { return } defer res.body.close() body, err := ioutil.readall(res.body) var re responsefrompost err = json.unmarshal(body, &re) fmt.println(re.enviar); }
with get:
error: &{%!e(string=array) %!e(*reflect.rtype=&{32 2509985895 0 8 8 25 0x608170 [0x7703c0 <nil>] 0x730b80 0x69acb0 0x6116c0 0x7732c0})}
the value sent server is:
[{"n_expediente":"9","enviar":"2"}]
how can use json variables?
the json array of objects having strings n_expediente
, enviar
on each instance. in go you'll need array of type;
re := []responsefrompost{} err := json.unmarshal([]byte(`[{"n_expediente":"9","enviar":"2"}]`), &re) fmt.println(re[0].enviar);
here's example show model needs https://play.golang.org/p/d64sict4ag
you'll want make other changes code based on that. loop bound length of slice ( for := range re { print }
) after unmarshal , different name type.
Comments
Post a Comment