【GO】go json 操作

golang json数据的转换

1、json bytes 转结构体

    
type CityWeather struct {
    Data WatherDetail
    Status int
    Desc string
}

type WatherDetail struct {
    Ysterday string
    City string
    Forecast []map[string]string
    Ganmao string
    Wendu string
}


func TestHttp3(t *testing.T){
    queryWeatherURL := "http://wthrcdn.etouch.cn/weather_mini?city=杭州"

    httpclient := &http.Client{}
    resp, err := httpclient.Get(queryWeatherURL)
    if err!=nil {
        fmt.Println(err)
        return
    }
    defer resp.Body.Close()
    cityWeather :=  new(CityWeather)
    _ = json.NewDecoder(resp.Body).Decode(cityWeather)

    fmt.Println(cityWeather.Data.City)

}

猜你喜欢

转载自www.cnblogs.com/jzsg/p/10923514.html
0条评论
添加一条新回复