如何解析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!

dongyi9330
dongyi9330 我尝试这样做,但是由于某种原因没有奏效。无论如何,感谢您的帮助。以下评论解决了我的问题。
3 年多之前 回复
doutang7661
doutang7661 您是否只需要在当前打印的末尾添加.A即可获得A字段?
3 年多之前 回复

1个回答



您可以简单地忽略不需要的任何结构。 JSON包将静默忽略源JSON中存在的,目标结构中不存在的任何内容。 因此,如果您不关心“数据”部分,请从结构中忽略它。 如果您仅在统计信息部分关注A和J,则仅包括这些内容。 </ p>

 类型response struct {
Response [] struct {
Stats struct {
intjson:“ a”
J float64json:“ j “
}json:” stats“
}json:” response“
}
</ code> </ pre>

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

编辑:同样值得 请注意,如果struct标记只是字段名称的小写字母,则无需包含这些标记,因为JSON包将匹配仅因大小写而不同的字段(不过,如果存在歧义,则首选完全匹配)。</ p>

</ div>

展开原文

原文

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).

dongyan1491
dongyan1491 太棒了,谢谢你 :)
3 年多之前 回复
doumeinuoye81969
doumeinuoye81969 如果J是接口{},则需要检查它是否为nil,然后将其断言为任何类型。 示例:play.golang.org/p/inXq03UDUL
3 年多之前 回复
dotn30471
dotn30471 如果A是“接口{}”而不是“ int”怎么办。 我的api调用有时会返回null值,我需要能够处理struct中的响应。 我知道如何将“ J” float64存储到变量中,但是,如果J为null,则会出现错误。 我将J float64更改为J interface {}。 但不确定如何将interface {}值存储到变量中
3 年多之前 回复
doxd96148
doxd96148 我对Go和这些结构很陌生,但这很棒。 有用。 非常感谢!
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐