gpt4 book ai didi

json - 从golang中的嵌套动态键json获取数据

转载 作者:IT王子 更新时间:2023-10-29 01:58:55 25 4
gpt4 key购买 nike

我是新来的,我正在尝试从 json 结构中提取特定的 key ,如下所示。

{
"cluster_name": "escluster",
"nodes": {
"Sd2AvEXsswjTn6ErRYjg": {
"timestamp": 1460624696217,
"name": "master1",
"transport_address": "10.0.0.1:9300",
"host": "10.0.0.1:9300",
"ip": [
"10.0.0.1:9300",
"NONE"
],
"attributes": {
"data": "false",
"master": "true"
},
"os": {
"timestamp": 1460624696217,
"cpu_percent": 0,
"load_average": 0,
"mem": {
"total_in_bytes": 163987664,
"free_in_bytes": 136357264,
"used_in_bytes": 26629400,
"free_percent": 84,
"used_percent": 16
},
"swap": {
"total_in_bytes": 0,
"free_in_bytes": 0,
"used_in_bytes": 0
}
}
},
"yzabB-OaTfOqvgAELIMq1Q": {
"timestamp": 1460624938213,
"name": "data_1",
"transport_address": "10.0.0.2:9300",
"host": "10.0.0.2",
"ip": [
"10.0.0.2:9300",
"NONE"
],
"attributes": {
"master": "false"
},
"os": {
"timestamp": 1460624938213,
"cpu_percent": 0,
"load_average": 0.29,
"mem": {
"total_in_bytes": 623840000,
"free_in_bytes": 4127648,
"used_in_bytes": 666012352,
"free_percent": 1,
"used_percent": 99
},
"swap": {
"total_in_bytes": 0,
"free_in_bytes": 0,
"used_in_bytes": 0
}
}
},
"q5CdgUF2TRewujr-0RPMgQ": {
"timestamp": 1460624934417,
"name": "master_0",
"transport_address": "10.0.0.3:9300",
"host": "10.0.0.2",
"ip": [
"10.0.0.3:9300",
"NONE"
],
"attributes": {
"data": "false",
"master": "true"
},
"os": {
"timestamp": 1460624934417,
"cpu_percent": 0,
"load_average": 0,
"mem": {
"total_in_bytes": 163898764,
"free_in_bytes": 139705616,
"used_in_bytes": 24194588,
"free_percent": 85,
"used_percent": 15
},
"swap": {
"total_in_bytes": 0,
"free_in_bytes": 0,
"used_in_bytes": 0
}
}
}
}
}

由于节点名称是动态的,我想为每个节点检索 mem。我找到了 this检索数据但不是动态 key 。我们如何在 go 中执行此操作。

最佳答案

学习如何在 Go 中处理数据的一个很好的引用:http://blog.golang.org/json-and-go

关于你的问题,在我看来你的数据结构相对良好......我在以下 Playground 示例中设置了一个提取每个节点的 mem 部分的示例:

http://play.golang.org/p/0O5U-3N_tA

它的要点是任何强指定都可以编码为struct。什么是动态的(例如你的节点名称)可以被编码为 map 。两者可以任意混合,因此:

type Node struct {
Timestamp uint64 `json:timestamp`
Name string `json:name`
TransportAddress string `json:transport_address`
Host string `json:host`
Ip []string `json:ip`
Attributes map[string]string `json:attributes`
Os *OsInfo `json:os`
}

type Payload struct {
Name string `json:cluster_name`
Nodes map[string]*Node `json:nodes`
}

可以捕获节点的一般结构(通过其 struct 定义)和它们被动态索引的事实(通过节点本身存储在动态 map 中的事实)

一旦解码,处理数据以提取内存信息就变得微不足道了:它只是众所周知的结构并一直向下映射:

var p Payload
if err := json.Unmarshal([]byte(payload), &p); err != nil {
log.Fatal(err)
}
for k, node := range p.Nodes {
fmt.Printf("%s: %s\n", k, node.Os.Mem)
}

预期的输出:

Sd2AvEXsswjTn6ErRYjg: map[total_in_bytes:%!s(uint64=163987664) free_in_bytes:%!s(uint64=136357264) used_in_bytes:%!s(uint64=26629400) free_percent:%!s(uint64=84) used_percent:%!s(uint64=16)]
yzabB-OaTfOqvgAELIMq1Q: map[used_percent:%!s(uint64=99) total_in_bytes:%!s(uint64=623840000) free_in_bytes:%!s(uint64=4127648) used_in_bytes:%!s(uint64=666012352) free_percent:%!s(uint64=1)]
q5CdgUF2TRewujr-0RPMgQ: map[total_in_bytes:%!s(uint64=163898764) free_in_bytes:%!s(uint64=139705616) used_in_bytes:%!s(uint64=24194588) free_percent:%!s(uint64=85) used_percent:%!s(uint64=15)]

(您当然可以先提取/重新格式化您的数据,并在完成后立即丢弃 Payload 对象)

关于json - 从golang中的嵌套动态键json获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36618679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com