2 回答
TA贡献1802条经验 获得超10个赞
这是一个示例保持界面:https : //play.golang.org/p/Eu6Hj6ddSE
只要改变: Charts interface{}女巫Charts []interface{} 。接口上,如果v是你ChartOptins然后让你用LEN()运算符
fmt.Println(len(v.Charts.([]interface{})))
TA贡献1788条经验 获得超4个赞
据我了解,您只需要定义用于正确映射 json 的结构。它可以是这样的:
package main
import (
"encoding/json"
"fmt"
"strings"
)
type ChartOptins struct {
Filters map[string]interface{} `json:"filters"`
Charts map[string][]Chart `json:"charts"`
}
type Chart struct {
Name string `json:"name"`
Type string `json:"type"`
}
func main() {
const jsonStream = `{
"filters": { "dayStart": "oneortwo", "dayEnd": "oneorthree", "timePeriods": [ [ "qqq", "www" ] ], "lines": [ "first", "avokado", "drunduki" ] },
"charts": { "noagg": [ { "name": "HOLD", "type": "line" }, { "name": "UKKR", "type": "line" }, { "name": "SVO", "type": "line" } ], "oracle": [ { "name": "TDD", "type": "line" } ] }
}`
var co ChartOptins
json.NewDecoder(strings.NewReader(jsonStream)).Decode(&co)
fmt.Println(co)
for k, v := range co.Charts {
fmt.Printf("key: %v\n", k)
for _, ch := range v {
fmt.Printf("Name: %v, type: %v\n", ch.Name, ch.Type)
}
}
}
- 2 回答
- 0 关注
- 175 浏览
添加回答
举报