type Stu struct {
Name string `json:"name"`
Age int
HIgh bool
sex string
Class *Class `json:"class"`
}
type Class struct {
Name string
Grade int
}
func main() {
//实例化一个数据结构,用于生成json字符串
stu := Stu{
Name: "张三",
Age: 18,
HIgh: true,
sex: "男",
}
//指针变量
cla := new(Class)
cla.Name = "1班"
cla.Grade = 3
stu.Class=cla
//Marshal失败时err!=nil
jsonStu, err := json.Marshal(stu)//Unmarshal
if err != nil {
fmt.Println("生成json字符串错误")
}
//jsonStu是[]byte类型,转化成string类型便于查看
fmt.Println(string(jsonStu))
}
package main
import (
"encoding/json"
"fmt"
)
type NginxInfo struct {
NginxConfigFile string `json:"nginx_config_file"`
NginxVersion string `json:"nginx_version"`
}
func main() {
//json字符中的"引号,需用\进行转义,否则编译出错
//json字符串沿用上面的结果,但对key进行了大小的修改,并添加了sex数据
data := "[{ \"nginx_version\": \"1.12.2\",\"nginx_config_file\": \"/etc/nginx/nginx.conf\"}, {\"nginx_version\": \"1.12.2\",\"nginx_config_file\": \"/root/nginx/conf/nginx.conf\"}]"
str := []byte(data)
//1.Unmarshal的第一个参数是json字符串,第二个参数是接受json解析的数据结构,第二个参数必须是指针,否 则无法接收解析的数据
//2.如果解析的目标为对象时var stu *PayLoad 或 stu:=new(PayLoad)
若果解析的目标为数组时var stu []*NginxInfo
var stu []*NginxInfo
err := json.Unmarshal(str, &stu)
//解析失败会报错,如json字符串格式不对,缺"号,缺}等。
if err != nil {
fmt.Println(err)
}
fmt.Println(stu)
}
解析json inline的使用
package main
import (
"encoding/json"
"fmt"
)
type Project struct {
Key string `json:"key"`
Value string `json:"value"`
}
type JiraHttpReqField struct {
Project `json:",inline"`
Summary string `json:"summary"`
Description string `json:"description"`
}
func main() {
dataProject := Project{
Key: "key",
Value: "value",
}
dataJiraHttpReqField := &JiraHttpReqField{
Project: dataProject,
Summary: "Summary",
Description: "Description",
}
data, _ := json.Marshal(dataJiraHttpReqField)
fmt.Println(string(data))
}
JiraHttpReqField 跟Project结构体是平级关系,如果很多struct需要Project里面的字段,可以直接inline Project,减少重复定义
"github.com/tidwall/gjson"
Jeffail/gabs#备用
package main
import (
"fmt"
"tidwall/gjson"
)
func main() {
msg := "{\"beat\":{\"address\":[\"192.168.10.203\",\"fe80::250:56ff:fea9:d3ad\"],\"hostname\":\"moban\",\"name\":\"moban\",\"version\":\"5.4.2\"},\"bizid\":0,\"cloudid\":0,\"counter\":34,\"data\":{\"data\":{\"bk_inst_key\":\"192.168.10.203-8001,8002-nginx\",\"bk_inst_name\":\"192.168.10.203-8001,8002-{\"bk_property_name\":\"\xe4\xb8\xbb\xe9\x85\x8d\xe7\xbd\xae\xe6\x96\x87\xe4\xbb\xb6\",\"bk_property_type\":\"longchar\",\"option\":null},\"hn_include_config\":{\"bk_property_name\":\"\xe5\xad\x90\xe9\x85\x8d\xe7\xbd\xae\xe6\x96\x87\xe4\xbb\xb6\",\"bk_property_type\":\"longchar\",\"option\":null},\"hn_ip\":{\"bk_property_name\":\"IP\xe5\x9c\xb0\xe5\x9d\x80\",\"bk_property_type\":\"singlechar\{\"bk_property_name\":\"Nginx\xe8\xbf\x9b\xe7\xa8\x8b\xe6\x95\xb0\",\"bk_property_type\":\"singlechar\",\"option\":null}},\"model\":{\"bk_classification_id\":\"bk_middleware\",\"bk_obj_id\":\"hn_nginx\",\"bk_obj_keys\":\"bk_ip,bk_inst_name\",\"bk_obj_name\":\"nginx\",\"bk_supplier_account\":\"\"}}},\"dataid\":1012,\"gseindex\":34,\"ip\":\"192.168.10.203\",\"timestamp\":\"2019-06-26T02:14:17.883Z\",\"type\":\"nginxbeat\"}"
ownerId := gjson.Get(msg, "data.data.bk_inst_name").String()
fmt.Println(ownerId)
}