如何拿到[]interface{}里面有的map

shenwei12303 · 2018-04-19 13:59:32 · 5047 次点击

通过断言为 []interface{} 类型,然后遍历,每个元素,又断言为 map

当然,这种的,也可以通过定义结构体解析或者使用 https://github.com/tidwall/gjson

#1
更多评论

这个API返回的json已经很简单了,直接定义结构体就可以解决你的问题

#2
type AutoGenerated struct {
    Data struct {
        Yesterday struct {
            Date string `json:"date"`
            High string `json:"high"`
            Fx   string `json:"fx"`
            Low  string `json:"low"`
            Fl   string `json:"fl"`
            Type string `json:"type"`
        } `json:"yesterday"`
        City     string `json:"city"`
        Aqi      string `json:"aqi"`
        Forecast []struct {
            Date      string `json:"date"`
            High      string `json:"high"`
            Fengli    string `json:"fengli"`
            Low       string `json:"low"`
            Fengxiang string `json:"fengxiang"`
            Type      string `json:"type"`
        } `json:"forecast"`
        Ganmao string `json:"ganmao"`
        Wendu  string `json:"wendu"`
    } `json:"data"`
    Status int    `json:"status"`
    Desc   string `json:"desc"`
}

惊了, 写这个API的人有点厉害。。。 Fx, Fengxiang, Fl, Fengli

#3