【发布时间】: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))
}
【问题讨论】: