golang json 处理方法

目的

利用 golang 进行定义,输出 json 格式
golang 其实对 json 格式定义建议使用 struct 格式, 比较清晰,方便

json 格式

常见 json 格式为 list , dict 两种

参考下面输出格式

dict

{
	"fstype": "ext4",
	"host": "host1"
}

list

["host1", "host2", "host3", "host4"]

dict + list

{
	"fstype": "ext4",
	"host": ["host1", "host2", "host3", "host4"]
}

list + dict

[{
		"fstype": "ext4",
		"host": "host1"
	},
	{
		"fstype": "ext4",
		"host": "host2"
}]

golang 输出 json 方法

dict

package main

import (
        "fmt"
        "encoding/json"
)


func makeExtr() map[string]string {           //  定义
        testExtr := make(map[string]string,0)
        testExtr["fstype"] = "ext4"
        testExtr["host"] = "host1"
        return testExtr
}

func buildExtr() string  {                     // json 输出
        hardExtr := makeExtr()
        jsonExtr, _ := json.Marshal(hardExtr)
        return fmt.Sprintf(string(jsonExtr))
}

func main() {
        info := buildExtr()
        fmt.Println(info)
}

输出结果

# go run json_001.go
{"fstype":"ext4","host":"host1"}

list

package main

import (
        "fmt"
        "encoding/json"
)


func makeExtr() []string {
        testExtr := []string{
                "host1",
                "host2",
                "host3",
                "host4",
        }
        return testExtr
}

func buildExtr() string  {
        hardExtr := makeExtr()
        jsonExtr, _ := json.Marshal(hardExtr)
        return fmt.Sprintf(string(jsonExtr))
}

func main() {
        info := buildExtr()
        fmt.Println(info)
}

执行结果如下

# go run json_002.go
["host1","host2","host3","host4"]

简单说明

json dict 使用 key: value 方法可以通过 map[string]string 进行定义
json list 直接通过 []string 进行定义
最简单直接方法进行 json 格式定义为 map[string]string
但假如 json 格式复杂时候,则很难维护

换个方式,我们使用 struct 进行重新定义上面例子

package main

import (
        "fmt"
        "encoding/json"
)

type HostInfo struct {
        FsType    string `json:"fstype"`
        HostName  string `json:"host"`
}

func makeExtr() HostInfo {
        var testExtr HostInfo
        testExtr.FsType = "ext4"
        testExtr.HostName= "host1"
        return testExtr
}

func buildExtr() string  {
        hardExtr := makeExtr()
        jsonExtr, _ := json.Marshal(hardExtr)
        return fmt.Sprintf(string(jsonExtr))
}

func main() {
        info := buildExtr()
        fmt.Println(info)
}

运行结果:

# go run json_003.go
{"fstype":"ext4","host":"host1"}

看上去比直接定义麻烦,但请参考下面例子

{
	"name": "monitorinfo",
	"value": [{
		"body": [{
			"metric": "cpu_idel",
			"datasourceId": 11,
			"from": 1587005847,
			"step": 60,
			"source": "123456789abcdefg",
			"to": 1587092247,
			"type": "Augix"
		}],
		"chart": {
			"legend": "myLegend",
			"title": "mytitile"
		},
		"url": "http://www.m3db.com"
	}]
}

思路

类似上述 json 格式,必须要通过 struct 进行解决才可以方便后续维护
struct 定义时候需要进行分组处理

参考 example

package main

import (
    "fmt"
    "encoding/json"
    "time"
)

type MonitorData struct {
        Name  string    `json:"name"`
        Value []M3Value `json:"value"`
}

type M3Body struct {
        Metric       string `json:"metric"`
        DatasourceID int    `json:"datasourceId"`
        From         int64  `json:"from"`
        Step         int    `json:"step"`
        Source       string `json:"source"`
        To           int64  `json:"to"`
        Type         string `json:"type"`
}

type M3Chart struct {
        Legend string `json:"legend"`
        Title  string `json:"title"`
}


type M3Value struct {
        Value   []M3Body   `json:"body"`
        Chart   M3Chart    `json:"chart"`
        URL     string     `json:"url"`
}

func buildM3Chart() M3Chart{
        var newM3Chart M3Chart
        newM3Chart.Legend = "myLegend"
        newM3Chart.Title = "mytitile"
        return newM3Chart
}

func buildM3Body() M3Body {
        var newBody M3Body
        newBody.Metric = "cpu_idel"
        newBody.DatasourceID = 11
        newBody.From = time.Now().Unix()
        newBody.Step = 60
        newBody.Source = "123456789abcdefg"
        newBody.To = time.Now().Unix() + 86400
        newBody.Type = "Augix"
        return newBody
}

func buildM3Value() M3Value {
        var newM3Value M3Value
        var m3BodySlice []M3Body
        m3BodyData := buildM3Body()
        m3BodySlice = append(m3BodySlice, m3BodyData)
        newM3Value.Value = m3BodySlice
        newM3Value.Chart = buildM3Chart()
        newM3Value.URL = "http://www.m3db.com"
        return newM3Value
}

func buildMonitorData() MonitorData {
        var newMonitorData MonitorData
        var monitorDataValue M3Value
        var monitorDataValueSlice []M3Value
        monitorDataValue = buildM3Value()
        newMonitorData.Name = "monitorinfo"
        monitorDataValueSlice = append(monitorDataValueSlice, monitorDataValue)
        newMonitorData.Value = monitorDataValueSlice
        return newMonitorData
}


func main() {

        jsonInfo := buildMonitorData()
        goJson, _ := json.Marshal(jsonInfo)
        data := fmt.Sprintf( string(goJson))
        fmt.Println(data)

}

思路

先对每个 dict 分别独立进行分层定义
在获取数据时候分别进行组合
注意一下 slice 的用法 (利用 struct + append 的方法实现)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Terry_Tsang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值