Go:Json转结构体

简介: Go:Json转结构体

解决实际需求,案例分享。

  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}
  1. 将响应结果(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
  1. 来自最好的总结

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

相关文章
|
1月前
|
XML JSON Go
掌握Go语言:Go语言结构体进阶,探索高级特性与实用技巧(23)
掌握Go语言:Go语言结构体进阶,探索高级特性与实用技巧(23)
|
1月前
|
Go 开发者
掌握Go语言:Go语言结构体,精准封装数据,高效管理实体对象(22)
掌握Go语言:Go语言结构体,精准封装数据,高效管理实体对象(22)
|
1月前
|
关系型数据库 MySQL Go
工厂模式+自动注册管理Go多包结构体
工厂模式+自动注册管理Go多包结构体
84 1
|
23小时前
|
Go
go结构体的定义
go结构体的定义
7 1
|
23小时前
|
JSON Go 数据格式
go之json使用
go之json使用
6 0
|
23小时前
|
Go
go反射获取变量类型、值、结构体成员、结构体方法
go反射获取变量类型、值、结构体成员、结构体方法
5 0
|
5天前
|
Shell Go
怎么让 Go 中如何让结构体不可比较?
在 Go 语言中,结构体默认是可以比较的,只要它们的所有字段都是可比较的。如果结构体包含不可比较的字段(如 `map`),则该结构体不能直接使用 `==` 进行比较,会引发编译错误。可以通过 `reflect.DeepEqual()` 函数对包含不可比较字段的结构体进行深度比较。为了禁止结构体的相等性比较,可以使用 `_ [0]func()` 作为匿名字段,这是一种不占用额外内存且具有明确语义的优雅做法。Go 语言标准库中也有类似的应用。
|
1月前
|
JSON JavaScript 前端开发
Golang深入浅出之-Go语言JSON处理:编码与解码实战
【4月更文挑战第26天】本文探讨了Go语言中处理JSON的常见问题及解决策略。通过`json.Marshal`和`json.Unmarshal`进行编码和解码,同时指出结构体标签、时间处理、omitempty使用及数组/切片区别等易错点。建议正确使用结构体标签,自定义处理`time.Time`,明智选择omitempty,并理解数组与切片差异。文中提供基础示例及时间类型处理的实战代码,帮助读者掌握JSON操作。
31 1
Golang深入浅出之-Go语言JSON处理:编码与解码实战
|
1月前
|
存储 JSON 缓存
Go语言学习9-结构体类型
【4月更文挑战第8天】本篇 Huazie 向大家介绍 Go语言的接口类型
304 8
Go语言学习9-结构体类型
|
26天前
|
存储 Go Python
Go 语言结构体
Go 语言结构体
9 0