Golang實(shí)現(xiàn)Json轉(zhuǎn)結(jié)構(gòu)體的示例詳解
解決實(shí)際需求,案例分享。
1.請求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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go 實(shí)現(xiàn)基于Token 的登錄流程深度分析
Token 認(rèn)證機(jī)制的核心思想是,服務(wù)端在用戶登錄時(shí)生成一個(gè) Token,客戶端在后續(xù)的請求中攜帶這個(gè) Token,服務(wù)端通過驗(yàn)證 Token 的有效性來確認(rèn)用戶的身份,本文將帶你深入探索基于 Token 的登錄流程,這是一種更為靈活且適用于現(xiàn)代應(yīng)用架構(gòu)的認(rèn)證方式2024-03-03
詳解Go中g(shù)in框架如何實(shí)現(xiàn)帶顏色日志
當(dāng)我們在終端上(比如Goland)運(yùn)行g(shù)in框架搭建的服務(wù)時(shí),會發(fā)現(xiàn)輸出的日志是可以帶顏色的,那這是如何實(shí)現(xiàn)的呢?本文就來和大家簡單講講2023-04-04
Go語言設(shè)計(jì)模式之實(shí)現(xiàn)觀察者模式解決代碼臃腫
今天學(xué)習(xí)一下用?Go?實(shí)現(xiàn)觀察者模式,觀察者模式主要是用來實(shí)現(xiàn)事件驅(qū)動編程。事件驅(qū)動編程的應(yīng)用還是挺廣的,除了我們都知道的能夠用來解耦:用戶修改密碼后,給用戶發(fā)短信進(jìn)行風(fēng)險(xiǎn)提示之類的典型場景,在微服務(wù)架構(gòu)實(shí)現(xiàn)最終一致性、實(shí)現(xiàn)事件源A?+?ES2022-08-08
淺析go語言如何實(shí)現(xiàn)協(xié)程的搶占式調(diào)度的
go語言通過GMP模型實(shí)現(xiàn)協(xié)程并發(fā),為了避免單協(xié)程持續(xù)持有線程導(dǎo)致線程隊(duì)列中的其他協(xié)程饑餓問題,設(shè)計(jì)者提出了一個(gè)搶占式調(diào)度機(jī)制,本文會基于一個(gè)簡單的代碼示例對搶占式調(diào)度過程進(jìn)行深入講解剖析2024-04-04
讓GPT教你用go語言和C語言開發(fā)IDE配置學(xué)習(xí)
這篇文章主要介紹了讓GPT教你用go語言和C語言開發(fā)IDE配置學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

