dongpan1365 2018-11-14 05:06
浏览 66
已采纳

如何处理Response JSON具有没有键的自定义字段?

Query Api and response a custom JSON, how to Unmarshal it. the sample JSON:

{"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu": {
"final_balance": 61914248289,
"n_tx": 3472,
"total_received": 3479994002972
}}

The key is a hex string. So how to handle it with golang convention, anyone can help me?

Below is my try test code:

c.OnResponse(func(r *colly.Response) {
    jsonData := r.Body

    fmt.Println(string(jsonData))
    fmt.Println("==================")

    //parse bitcoin json
    jsonMap := make(map[string]interface{})
    err := json.Unmarshal([]byte(jsonData), &jsonMap)
    if err != nil {
        panic(err)
    }

    fmt.Println(jsonMap)

    dumpMap("", jsonMap)
})

func dumpMap(space string, m map[string]interface{}) {
    for k, v := range m {
        if mv, ok := v.(map[string]interface{}); ok {
            fmt.Printf("{ \"%v\": 
", k)
            dumpMap(space+"\t", mv)
            fmt.Printf("}
")
        } else {
            fmt.Printf("%v %v : %v
", space, k, v)
        }
    }
}

and go run cmd/main.go, the console is print here:

{"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu": {
    "final_balance": 75494521080,
    "n_tx": 3493,
    "total_received": 3493574275763
}}
==================
map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:map[n_tx:3493 total_received:3.493574275763e+12 final_balance:7.549452108e+10]]
{ "14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
         final_balance : 7.549452108e+10
         n_tx : 3493
         total_received : 3.493574275763e+12
}

Do I need customised unmarshal func to get string key? If I use 14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu as key I can't easily to access. I just want to know how handle it.

  • 写回答

1条回答 默认 最新

  • dqrfdl5708 2018-11-14 05:38
    关注

    you can unmarshal it into map, so you can get generated key as a key of map https://play.golang.org/p/IfEjjvKakpu

    package main
    
    import (
        "encoding/json"
        "fmt"
        "log"
    )
    
    var input = `{"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu": {
    "final_balance": 61914248289,
    "n_tx": 3472,
    "total_received": 3479994002972
    }}`
    
    type object struct {
        FinalBalance uint64 `json:"final_balance"`
        NTX uint64 `json:"n_tx"`
        TotalReceived uint64 `json:"total_received"`
    }
    
    func main() {
        var result map[string]object;
        err := json.Unmarshal([]byte(input), &result);
        if err != nil {
            log.Fatal(err)
        }
    
        fmt.Printf("result: %+v", result)
        // result: map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:{FinalBalance:61914248289 NTX:3472 TotalReceived:3479994002972}]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP