douci2022 2019-01-14 06:12
浏览 55
已采纳

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

I'm trying to parse JSON from API in golang code. Of passed with true option argument it gives different additional info and with false different output. I have covered that in the following golang play link: https://play.golang.org/p/-JffO4AS01N

I need to parse the value of variable mtJson.

Used the Json to Go (https://mholt.github.io/json-to-go/) converted to get help in creating the struct type for this. But it was giving following struct type for the json example:

{
"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"`
}

This doesn't seem right.

The value of the string hash key will be always different not determined, so can not just set that as is in struct.

I'm feeling confused on how to parse the JSON so that I can finally get the values like this:


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

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

Please help

I have covered that in the following golang play link: https://play.golang.org/p/-JffO4AS01N

I have covered that in the following golang play link: https://play.golang.org/p/-JffO4AS01N

Expected: fmt.Println(mt.Result.("99c4d91acc2486955c98015fbbdf06239b983c9d93d5069c39d040702af88738").Size) 845

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

Actual Result: {{0 0 0 0 0 0 []}}

  • 写回答

1条回答 默认 最新

  • dougong5817 2019-01-14 06:19
    关注

    Since the keys are not known, you have to resort to a dynamic data structure.

    Define the single element like:

    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"`
    }
    

    Then parse your json to a map[string]Element like so:

    result := make(map[string]Element)
    json.Unmarshal(jsonBytes, &result)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?