Golang怎么实现Json转结构体

前端开发   发布日期:2023年07月11日   浏览次数:475

这篇文章主要讲解了“Golang怎么实现Json转结构体”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Golang怎么实现Json转结构体”吧!

1.请求Zabbix API,通过itemid获取到AppName(应用集名称)

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "strings"
  9. )
  10. func PostRequest(payload string, url string) {
  11. method := "POST"
  12. pl := strings.NewReader(payload)
  13. client := &http.Client{}
  14. req, err := http.NewRequest(method, url, pl)
  15. if err != nil {
  16. fmt.Println(err)
  17. return
  18. }
  19. req.Header.Add("Content-Type", "application/json")
  20. res, err := client.Do(req)
  21. if err != nil {
  22. fmt.Println(err)
  23. return
  24. }
  25. defer res.Body.Close()
  26. body, err := ioutil.ReadAll(res.Body)
  27. if err != nil {
  28. log.Println(err)
  29. return
  30. }
  31. fmt.Println(string(body))
  32. }
  33. func main() {
  34. const api = "http://192.168.11.11:28080/api_jsonrpc.php"
  35. const token = "a638200c24a8bea7f78cd5cabf3d1dd5"
  36. const itemid = "33918"
  37. a := fmt.Sprintf(`{
  38. "jsonrpc": "2.0",
  39. "method": "application.get",
  40. "params": {"itemids": "%s"},
  41. "auth": "%s","id": 2
  42. }`, itemid, token)
  43. PostRequest(a, api)
  44. }

响应结果:

{"jsonrpc":"2.0","result":[{"applicationid":"1574","hostid":"10354","name":"TEST","flags":"0","templateids":[]}],"id":2}

2.将响应结果(json)转结构体,方便取值

在原来代码的基础上,继续编码。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "strings"
  9. )
  10. type resultInfo struct {
  11. Applicationid string `json:"applicationid"`
  12. Hostid string `json:"hostid"`
  13. Name string `json:"name"`
  14. Flags string `json:"flags"`
  15. Templateids []string `json:"templateids"`
  16. }
  17. type resultArr []resultInfo
  18. type Response struct {
  19. Jsonrpc string `json:"jsonrpc"`
  20. Result resultArr `json:result`
  21. Id int `json:"id"`
  22. }
  23. type Byte []byte
  24. func JsonConvertStruct(body Byte) {
  25. var response Response
  26. json.Unmarshal([]byte(body), &response)
  27. fmt.Println(response.Result[0].Name)
  28. }
  29. func PostRequest(payload string, url string) {
  30. method := "POST"
  31. pl := strings.NewReader(payload)
  32. client := &http.Client{}
  33. req, err := http.NewRequest(method, url, pl)
  34. if err != nil {
  35. fmt.Println(err)
  36. return
  37. }
  38. req.Header.Add("Content-Type", "application/json")
  39. res, err := client.Do(req)
  40. if err != nil {
  41. fmt.Println(err)
  42. return
  43. }
  44. defer res.Body.Close()
  45. body, err := ioutil.ReadAll(res.Body)
  46. if err != nil {
  47. log.Println(err)
  48. return
  49. }
  50. JsonConvertStruct(body)
  51. }
  52. func main() {
  53. const api = "http://192.168.11.11:28080/api_jsonrpc.php"
  54. const token = "a638200c24a8bea7f78cd5cabf3d1dd5"
  55. const itemid = "33918"
  56. a := fmt.Sprintf(`{
  57. "jsonrpc": "2.0",
  58. "method": "application.get",
  59. "params": {"itemids": "%s"},
  60. "auth": "%s","id": 2
  61. }`, itemid, token)
  62. PostRequest(a, api)
  63. }

结果:

TEST

以上就是Golang怎么实现Json转结构体的详细内容,更多关于Golang怎么实现Json转结构体的资料请关注九品源码其它相关文章!