【问题标题】:How do I access certain values in this struct [duplicate]如何访问此结构中的某些值 [重复]
【发布时间】:2020-12-16 00:57:56
【问题描述】:

我在 Go 中记录一些东西。这是值,下面是我记录 reflect.TypeOf(attributes.Pdp.SellableUnits[i].Attributes) 时的结果:

[{22555278 val 03.5}]
[{22554867 val 04.0}]
[{22555002 val 04.5}]
[{22555279 val 05.0}]
[{22555280 val 05.5}]
[{22555144 val 06.0}]
[{22555145 val 06.5}]
[{22555146 val 07.0}]

// TypeOf
[1]struct { ID string "json:\"id\""; Type string "json:\"type\""; Value string "json:\"value\"" }
[1]struct { ID string "json:\"id\""; Type string "json:\"type\""; Value string "json:\"value\"" }
[1]struct { ID string "json:\"id\""; Type string "json:\"type\""; Value string "json:\"value\"" }
[1]struct { ID string "json:\"id\""; Type string "json:\"type\""; Value string "json:\"value\"" }
[1]struct { ID string "json:\"id\""; Type string "json:\"type\""; Value string "json:\"value\"" }
[1]struct { ID string "json:\"id\""; Type string "json:\"type\""; Value string "json:\"value\"" }
[1]struct { ID string "json:\"id\""; Type string "json:\"type\""; Value string "json:\"value\"" }
[1]struct { ID string "json:\"id\""; Type string "json:\"type\""; Value string "json:\"value\"" }

我希望能够只记录 ID 字符串,在这种情况下是代码块顶部的许多数字的字符串(22555278、22554867、22555002 等...)

这是我记录所有这些的代码

// Struct
type sizeJ struct {
    Pdp struct {
        Units []struct {
            Attributes [1]struct {
                ID    string `json:"id"`
                Type  string `json:"type"`
                Value string `json:"value"`
            } `json:"attributes"`
        } `json:"Units"`
    } `json:"pdp"`
}

// ...
        body, _ := ioutil.ReadAll(resp.Body)

        xml := strings.NewReader(string(body))

        j, _ := xj.Convert(xml)
        if err != nil {
            log.Fatal(err)
        }

        var attributes sizeJ
        json.Unmarshal([]byte(j.String()), &attributes)

        for i := 0; i < len(attributes.Pdp.Units); i++ {
            fmt.Println(reflect.TypeOf(attributes.Pdp.Units[i].Attributes))

        }

【问题讨论】:

    标签: json go


    【解决方案1】:

    您的类型声明为:

    type sizeJ struct {
        Pdp struct {
            Units []struct {
                Attributes [1]struct {
                    ID    string `json:"id"`
                    Type  string `json:"type"`
                    Value string `json:"value"`
                } `json:"attributes"`
            } `json:"Units"`
        } `json:"pdp"`
    }
    

    您可以只打印 ID:

    for i := 0; i < len(attributes.Pdp.Units); i++ {
        // fmt.Println(reflect.TypeOf(attributes.Pdp.Units[i].Attributes))
        fmt.Printf("ID: %s\n", attributes.Pdp.Units[i].Attributes[0].ID)
    
    }
    

    【讨论】:

    • attributes.Pdp.SellableUnits[i].Attributes.ID undefined (type [1]struct{ID string "json:\"id\""; Type string "json:\"type\" "; 值字符串 "json:\"value\""} 没有字段或方法 ID)
    • 将我的结构也添加到主帖
    猜你喜欢
    • 2021-06-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多