如何解析字段名称中具有随机哈希值的golangjson数组



我正在尝试解析golang代码中API的JSON。与true选项参数一起传递的,它提供了不同的附加信息,与false一起传递的输出不同。我已经在下面的高尔夫游戏链接中介绍了这一点:https://play.golang.org/p/-JffO4AS01N

我需要解析变量mtJson的值。

用Json去(https://mholt.github.io/json-to-go/)已转换以获取创建此结构类型的帮助。但它为json示例提供了以下结构类型:

{
"result": {
"99c4d91acc2486955c98015fbbdf06239b983c9d93d5069c39d040702af88738": {
"size": 845,
"fee": 0.000144,
"time": 1547444481,
"height": 1183405,
"startingpriority": 89509.20245398773,
"currentpriority": 89509.20245398773,
"depends": []
},
"73f582cf419f8b1cd6a87f81e0e9a4e783add27c2be083361e8eb4a3bac0134e": {
"size": 1635,
"fee": 0.000312,
"time": 1547444435,
"height": 1183405,
"startingpriority": 341863.3540372671,
"currentpriority": 341863.3540372671,
"depends": []
}
},
"error": null,
"id": "curltest"
}
type AutoGenerated struct {
Result struct {
Nine9C4D91Acc2486955C98015Fbbdf06239B983C9D93D5069C39D040702Af88738 struct {
Size             int           `json:"size"`
Fee              float64       `json:"fee"`
Time             int           `json:"time"`
Height           int           `json:"height"`
Startingpriority float64       `json:"startingpriority"`
Currentpriority  float64       `json:"currentpriority"`
Depends          []interface{} `json:"depends"`
} `json:"99c4d91acc2486955c98015fbbdf06239b983c9d93d5069c39d040702af88738"`
Seven3F582Cf419F8B1Cd6A87F81E0E9A4E783Add27C2Be083361E8Eb4A3Bac0134E struct {
Size             int           `json:"size"`
Fee              float64       `json:"fee"`
Time             int           `json:"time"`
Height           int           `json:"height"`
Startingpriority float64       `json:"startingpriority"`
Currentpriority  float64       `json:"currentpriority"`
Depends          []interface{} `json:"depends"`
} `json:"73f582cf419f8b1cd6a87f81e0e9a4e783add27c2be083361e8eb4a3bac0134e"`
} `json:"result"`
Error interface{} `json:"error"`
ID    string      `json:"id"`
}

这似乎不对。

字符串哈希键的值在未确定的情况下总是不同的,因此不能仅按结构中的原样设置。

我对如何解析JSON感到困惑,以便最终获得如下值:


fmt.Println(mt.Result.("99c4d91acc2486955c98015fbbdf06239b983c9d93d5069c39d040702af88738").Size)
fmt.Println(mt.Result.("99c4d91acc2486955c98015fbbdf06239b983c9d93d5069c39d040702af88738").Fee)

请帮助

我已经在下面的高尔夫游戏链接中介绍了这一点:https://play.golang.org/p/-JffO4AS01N

我已经在下面的高尔夫游戏链接中介绍了这一点:https://play.golang.org/p/-JffO4AS01N

预期:fmt。Println(mt.Result.("99c4d91acc2486955c98015fbbdf06239b983c9d93d5069c39d040702af88738").尺寸)845

fmt.Println(mt.Result.("99c4d91acc2486955c98015fbbdf06239b983c9d93d5069c39d040702af88738").费用)0.000144

实际结果:{{0 0 0 0 0[]}}

由于键是未知的,因此必须采用动态数据结构。

定义单个元素,如:

type Element struct {
Size             int           `json:"size"`
Fee              float64       `json:"fee"`
Time             int           `json:"time"`
Height           int           `json:"height"`
Startingpriority float64       `json:"startingpriority"`
Currentpriority  float64       `json:"currentpriority"`
Depends          []interface{} `json:"depends"`
}

然后将json解析为map[string]Element,如下所示:

result := make(map[string]Element)
json.Unmarshal(jsonBytes, &result)

最新更新