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

}

“年轻时,我没受过多少系统教育,但什么书都读。读得最多的是诗,包括烂诗,我坚信烂诗早晚会让我邂逅好诗。” by. 马尔克斯
原文地址:https://www.cnblogs.com/jzsg/p/10923514.html