前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Go:Json转结构体

Go:Json转结构体

作者头像
不背锅运维
发布2022-10-18 14:40:43
发布2022-10-18 14:40:43
5740
举报
文章被收录于专栏:监控监控

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

  1. 请求Zabbix API,通过itemid获取到AppName(应用集名称)
代码语言:go
复制
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)
}

响应结果:

代码语言:json
复制
{"jsonrpc":"2.0","result":[{"applicationid":"1574","hostid":"10354","name":"TEST","flags":"0","templateids":[]}],"id":2}
  1. 将响应结果(json)转结构体,方便取值在原来代码的基础上,继续编码。
代码语言:go
复制
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)
}

结果:

代码语言:text
复制
TEST
  1. 来自最好的总结人生苦短,建议你还是用python吧!

本文转载于(喜欢的盆友关注我们):https://mp.weixin.qq.com/s/mBbf0DuUh1Af3vi2DBE7DA

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档