dongshicuo4844 2017-02-06 21:19
浏览 30
已采纳

如何解析golang中的结构并从该结构中打印项目?

Here is a similar example: Parsing JSON in GoLang into struct I am getting a json response from the server and I only need to get certain data. I've created a sample code:

package main

import (
    "fmt"
    "encoding/json"
)

type response struct {
    Response []struct {
        Stats struct {
            A int `json:"a"`
            B float64 `json:"b"`
            C int `json:"c"`
            D float64 `json:"d"`
            E float64 `json:"e"`
            F float64 `json:"f"`
            G float64 `json:"g"`
            H float64 `json:"h"`
            I float64 `json:"i"`
            J float64 `json:"j"`
        } `json:"stats"`
        Data []struct {
            Num0 int64 `json:"0"`
            Num1 interface{} `json:"1"`
        } `json:"data"`
    } `json:"response"`
}


func main() {
    src := `
{
    "response": [{
            "stats": {
                "a": 458,
                "b": 302.3738,
                "c": 0,
                "d": 706.777,
                "e": 2.423,
                "f": 238.73375,
                "g": 68.971,
                "h": 85.1989781659389,
                "i": 84.6381777592766,
                "j": 292658.49
            },
            "data": [
                [1453222860000, null],
                [1453223160000, 3.769],
                [1453223220000, 37.464]
            ]
        }
    ]
}
`

    var g response
    json.Unmarshal([]byte(src), &g)
    fmt.Println(g.Response[0].Stats)  
}

The output I get is

`{458 302.3738 0 706.777 2.423 238.73375 68.971 85.1989781659389 84.6381777592766 292658.49}

`

I want to get certain items from just the stat struct. Let's say I want to print the value of A or J from the stats struct. How do I do that? I don't really need the data struct. The json response I got from the server gave me those data but I dont really need it. Anyway, my question is how do I get only certain items only in the Stats struct?

Any suggestion on how to do this or even improve my structs format? Thanks!

  • 写回答

1条回答 默认 最新

  • dongll0502 2017-02-06 21:25
    关注

    You can simply omit any piece of the structure you don't care about. The JSON package will silently ignore anything present in the source JSON that isn't present in the destination structure. Thus if you don't care about the Data portion, omit it from the structure. If you only care about A and J in the stats section, only include those.

    type response struct {
        Response []struct {
            Stats struct {
                A int `json:"a"`
                J float64 `json:"j"`
            } `json:"stats"`
        } `json:"response"`
    }
    

    https://play.golang.org/p/W3bmlf15sF

    Edit: Also worth noting that you don't need to include the struct tags if they are just lowercases of the field names, as the JSON package will match fields that only differ by capitalization (though it prefers exact matches, if there's ambiguity).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题