登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-23 11:45:33 268浏览 收藏

怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《如何解析字段名称中具有随机哈希值的golang json数组》,涉及到,有需要的可以收藏一下

问题内容

我正在尝试从 golang 代码中的 api 解析 json。 通过 true 选项参数传递,它提供不同的附加信息,并通过 false 提供不同的输出。 我在以下 golang 播放链接中介绍了这一点: https://play.golang.org/p/-jffo4as01n

我需要解析变量 mtjson 的值。

使用转换后的 json to go (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)

请帮忙

我在以下 golang 播放链接中介绍了这一点: https://play.golang.org/p/-jffo4as01n

我在以下 golang 播放链接中介绍了这一点: https://play.golang.org/p/-jffo4as01n

预计: fmt.println(mt.result.("99c4d91acc2486955c98015fbbdf06239b983c9d93d5069c39d040702af88738").size) 845

fmt.println(mt.result.("99c4d91acc2486955c98015fbbdf06239b983c9d93d5069c39d040702af88738").费用) 0.000144

实际结果:{{0 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)

今天关于《如何解析字段名称中具有随机哈希值的golang json数组》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>