简单介绍Golang实现Json转结构体的方法

Crq
Crq
管理员
1566
文章
0
粉丝
Linux教程评论6字数 413阅读1分22秒阅读模式
摘要这篇文章主要为大家详细介绍了Golang实现Json转结构体的方法,文中的示例代码讲解详细,对学习Go语言有一定的帮助,需要的可以参考一下
解决实际需求,案例分享。
1.请求Zabbix API,通过itemid获取到AppName(应用集名称)
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)
}

响应结果:

{"jsonrpc":"2.0","result":[{"applicationid":"1574","hostid":"10354","name":"TEST","flags":"0","templateids":[]}],"id":2}
2.将响应结果(json)转结构体,方便取值

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

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)
}

结果:

TEST
3.来自最好的总结

人生苦短,建议你还是用python吧!

到此这篇关于Golang实现Json转结构体的示例详解的文章就介绍到这了

weinxin
我的微信
微信号已复制
我的微信
这是我的微信扫一扫
 
Crq
  • 本文由 Crq 发表于2025年2月3日 21:17:35
  • 转载请注明:https://www.cncrq.com/12899.html
简单介绍Go 语言单例模式 Linux教程

简单介绍Go 语言单例模式

这篇文章主要为大家介绍了Go 语言单例模式示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证