【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)

}

posted @ 2019-05-25 18:52  加州水果  阅读(303)  评论(0编辑  收藏  举报