Golang實(shí)現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體的示例詳解
解決實(shí)際需求,案例分享。
1.請(qǐng)求Zabbix API,通過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)體,方便取值
在原來代碼的基礎(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
3.來自最好的總結(jié)
人生苦短,建議你還是用python吧!
到此這篇關(guān)于Golang實(shí)現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體的示例詳解的文章就介紹到這了,更多相關(guān)Golang Json轉(zhuǎn)結(jié)構(gòu)體內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang教程之不可重入函數(shù)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Golang教程之不可重入函數(shù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
VS Code配置Go語言開發(fā)環(huán)境的詳細(xì)教程
這篇文章主要介紹了VS Code配置Go語言開發(fā)環(huán)境的詳細(xì)教程,本文通過實(shí)例代碼圖文相結(jié)合的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
golang實(shí)戰(zhàn)之truncate日志文件詳解
這篇文章主要給大家介紹了關(guān)于golang實(shí)戰(zhàn)之truncate日志文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Go?并發(fā)編程協(xié)程及調(diào)度機(jī)制詳情
這篇文章主要介紹了Go并發(fā)編程協(xié)程及調(diào)度機(jī)制詳情,協(xié)程是Go語言最大的特色之一,goroutine的實(shí)現(xiàn)其實(shí)是通過協(xié)程,更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-09-09

