gpt4 book ai didi

json - 构建一个 golang 结构来存储来自已解析的 JSON 文件的数据

转载 作者:IT王子 更新时间:2023-10-29 02:09:22 26 4
gpt4 key购买 nike

我正在尝试加载一个相当大的 JSON 文件,其中包含 x 个 JSON 数组。我遇到的问题是下面数据中的每个节点都有一个唯一的名称,所以我不确定如何构建结构来存储它们。

这是来自 JSON 文件的数据片段(这个文件可以有更多的节点,下面只有两个节点)

       {
"timestamp": 1533397325,
"total_nodes": 9522,
"latest_height": 535196,
"nodes": {
"220.75.229.130:3927": [
70015,
"/Satoshi:0.13.2/",
1530858117,
13,
165277,
"220.75.229.130",
"Seoul",
"KR",
37.5985,
126.9783,
"Asia/Seoul",
"AS4766",
"Korea Telecom"
],
"192.162.102.68:8333": [
70015,
"/Satoshi:0.15.1/",
1533061934,
13,
535196,
"192.162.102.68",
null,
"RU",
55.7386,
37.6068,
null,
"AS50113",
"MediaServicePlus LLC"
]
}
}

到目前为止,这是我的代码和 Go 中的 JSON 对象:

type MyJsonName struct {
LatestHeight int `json:"latest_height"`
Timestamp int `json:"timestamp"`
TotalNodes int `json:"total_nodes"`
Nodes struct {
Data string
} `json:"nodes"`
}

func main() {
jsonFile, err := os.Open("someFile")
if err != nil {
fmt.Println(err)
}
byteValue, _ := ioutil.ReadAll(jsonFile)
var MyJSONANE MyJsonName
err = json.Unmarshal(byteValue, &MyJSONANE)
if err != nil {
fmt.Println(err)
}
fmt.Println(MyJSONANE)
}

此代码运行良好,我的问题是节点中的数据从未填充 JSON 数组及其内容。

这是输出:{535196 1533397325 9522 {}}

任何帮助调整我的结构以读取 JSON 数组的人都将不胜感激。我只想强调节点中 JSON 数组的名称是唯一的,并且可以有 total_nodes 个。

最佳答案

使用 map[string]interface{} 来存储节点数据。对于具有动态键的未知 json 数据,接口(interface)是最好的,它将帮助您存储任何类型的数据。

package main

import (
"fmt"
"encoding/json"
)

type MyJsonName struct {
LatestHeight int `json:"latest_height"`
Timestamp int `json:"timestamp"`
TotalNodes int `json:"total_nodes"`
Nodes map[string]interface{}
}

var byteValue string = `{
"timestamp": 1533397325,
"total_nodes": 9522,
"latest_height": 535196,
"nodes": {
"220.75.229.130:3927": [
70015,
"/Satoshi:0.13.2/",
1530858117,
13,
165277,
"220.75.229.130",
"Seoul",
"KR",
37.5985,
126.9783,
"Asia/Seoul",
"AS4766",
"Korea Telecom"
],
"192.162.102.68:8333": [
70015,
"/Satoshi:0.15.1/",
1533061934,
13,
535196,
"192.162.102.68",
null,
"RU",
55.7386,
37.6068,
null,
"AS50113",
"MediaServicePlus LLC"
]
}
}`

func main() {
var MyJSONANE MyJsonName
err := json.Unmarshal([]byte(byteValue), &MyJSONANE)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%+v",MyJSONANE)
}

Go playground 上的工作代码

要从 interface{} 获取基础值,您需要对每种类型进行类型断言。您可以通过 switch 递归地从接口(interface)获取值。

func fetchValue(value interface{}) {
switch value.(type) {
case string:
fmt.Printf("%v is an string \n ", value.(string))
case bool:
fmt.Printf("%v is bool \n ", value.(bool))
case float64:
fmt.Printf("%v is float64 \n ", value.(float64))
case []interface{}:
fmt.Printf("%v is a slice of interface \n ", value)
for _, v := range value.([]interface{}) {
fetchValue(v)
}
case map[string]interface{}:
fmt.Printf("%v is a map \n ", value)
for _, v := range value.(map[string]interface{}) {
fetchValue(v)
}
default:
fmt.Printf("%v is unknown \n ", value)
}
}

关于json - 构建一个 golang 结构来存储来自已解析的 JSON 文件的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51809934/

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