Home > front end >  I have a GoLang JSON Fetch, sending POST Request Question
I have a GoLang JSON Fetch, sending POST Request Question

Time:06-25

I am receiving a JSON request. How do I parse it into individual variables so I can use them for SQL requests (and other things)?

I need "{'name':'jude','address':'456 main'}" converted into a name variable and an address variable.

I got this far...

type Member struct {
       Name string `json:"name"`
       Address string `json:"address"`
}
   
jsonBody, _ := ioutil.ReadAll(req.Body)

But I can't get the individual fields out of the json body.

Thank you in advance.

CodePudding user response:

    member := &Member{}
    err := json.Unmarshal(jsonBody, member)

    fmt.Println(member.Name)
    fmt.Println(member.Address)
}

Try the example here: https://go.dev/play/p/2t2KEBiVRDF?v=goprev

  • Related