溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Golang如何實(shí)現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體

發(fā)布時(shí)間:2023-02-20 09:25:16 來(lái)源:億速云 閱讀:102 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“Golang如何實(shí)現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Golang如何實(shí)現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體”吧!

1.請(qǐng)求Zabbix API,通過(guò)itemid獲取到AppName(應(yīng)用集名稱)

package main

import (
 "encoding/json"
 "fmt"
 "io/ioutil"
 "log"
 "net/http"
 "strings"
)

func PostRequest(payload string, url string) {
 method := "POST"
 pl := strings.NewReader(payload)
 client := &http.Client{}
 req, err := http.NewRequest(method, url, pl)

 if err != nil {
  fmt.Println(err)
  return
 }
 req.Header.Add("Content-Type", "application/json")

 res, err := client.Do(req)
 if err != nil {
  fmt.Println(err)
  return
 }
 defer res.Body.Close()

 body, err := ioutil.ReadAll(res.Body)

 if err != nil {
  log.Println(err)
  return
 }
 fmt.Println(string(body))
}

func main() {
 const api = "http://192.168.11.11:28080/api_jsonrpc.php"
 const token = "a638200c24a8bea7f78cd5cabf3d1dd5"
 const itemid = "33918"

 a := fmt.Sprintf(`{
  "jsonrpc": "2.0",
  "method": "application.get",
  "params": {"itemids": "%s"},
  "auth": "%s","id": 2
  }`, itemid, token)

 PostRequest(a, api)
}

響應(yīng)結(jié)果:

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

2.將響應(yīng)結(jié)果(json)轉(zhuǎn)結(jié)構(gòu)體,方便取值

在原來(lái)代碼的基礎(chǔ)上,繼續(xù)編碼。

package main

import (
 "encoding/json"
 "fmt"
 "io/ioutil"
 "log"
 "net/http"
 "strings"
)

type resultInfo struct {
 Applicationid string   `json:"applicationid"`
 Hostid        string   `json:"hostid"`
 Name          string   `json:"name"`
 Flags         string   `json:"flags"`
 Templateids   []string `json:"templateids"`
}

type resultArr []resultInfo

type Response struct {
 Jsonrpc string    `json:"jsonrpc"`
 Result  resultArr `json:result`
 Id      int       `json:"id"`
}

type Byte []byte

func JsonConvertStruct(body Byte) {
 var response Response
 json.Unmarshal([]byte(body), &response)
 fmt.Println(response.Result[0].Name)
}

func PostRequest(payload string, url string) {
 method := "POST"
 pl := strings.NewReader(payload)
 client := &http.Client{}
 req, err := http.NewRequest(method, url, pl)

 if err != nil {
  fmt.Println(err)
  return
 }
 req.Header.Add("Content-Type", "application/json")

 res, err := client.Do(req)
 if err != nil {
  fmt.Println(err)
  return
 }
 defer res.Body.Close()

 body, err := ioutil.ReadAll(res.Body)

 if err != nil {
  log.Println(err)
  return
 }
 JsonConvertStruct(body)
}

func main() {
 const api = "http://192.168.11.11:28080/api_jsonrpc.php"
 const token = "a638200c24a8bea7f78cd5cabf3d1dd5"
 const itemid = "33918"

 a := fmt.Sprintf(`{
  "jsonrpc": "2.0",
  "method": "application.get",
  "params": {"itemids": "%s"},
  "auth": "%s","id": 2
  }`, itemid, token)

 PostRequest(a, api)
}

結(jié)果:

TEST

感謝各位的閱讀,以上就是“Golang如何實(shí)現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Golang如何實(shí)現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI