golang读取json格式的天气预报

golang读取json格式的天气预报,第1张

概述使用天气预报的接口:http://weather.china.xappengine.com/api?city=南宁 (找了好久才找到一个支持拼音的,不过好像小地方是没数据的) 访问得到json格式压缩的数据,格式化后 { "pub": "2013-06-29 22:59", "name": "南宁", "wind": { "chill": 81,

使用天气预报的接口:http://weather.china.xappengine.com/api?city=南宁 (找了好久才找到一个支持拼音的,不过好像小地方是没数据的)

访问得到Json格式压缩的数据,格式化后


{    "pub": "2013-06-29 22:59","name": "南宁","wind": {        "chill": 81,"direction": 140,"speed": 7    },"astronomy": {        "sunrise": "6:05","sunset": "19:34"    },"atmosphere": {        "humIDity": 89,"visibility": 6.21,"pressure": 29.71,"rising": 1    },"forecasts": [        {            "date": "2013-06-29","day": 6,"code": 29,"text": "局部多云","low": 26,"high": 32,"image_large": "http://weather.china.xappengine.com/static/w/img/d29.png","image_small": "http://weather.china.xappengine.com/static/w/img/s29.png"        },{            "date": "2013-06-30","day": 0,"code": 30,"low": 25,"high": 33,"image_large": "http://weather.china.xappengine.com/static/w/img/d30.png","image_small": "http://weather.china.xappengine.com/static/w/img/s30.png"        },{            "date": "2013-07-01","day": 1,"code": 37,"text": "局部雷雨","low": 24,"image_large": "http://weather.china.xappengine.com/static/w/img/d37.png","image_small": "http://weather.china.xappengine.com/static/w/img/s37.png"        },{            "date": "2013-07-02","day": 2,"code": 38,"text": "零星雷雨","image_large": "http://weather.china.xappengine.com/static/w/img/d38.png","image_small": "http://weather.china.xappengine.com/static/w/img/s38.png"        },{            "date": "2013-07-03","day": 3,"image_small": "http://weather.china.xappengine.com/static/w/img/s38.png"        }    ]}
GO语言内置的enCoding/Json标准库,Json.Unmarshal()可以解析Json数据

约定:Json中的布尔值会解析为布尔值,数字(整数,浮点型)解析为float64,string解析为string,数组解析为接口数组,空值解析为nil。 这些字段在类型声明中必须都是以大写字母开头、可被导出的字段。

Json.Unmarshal()函数会根据一个约定的顺序查找目标结构中的字段,如果找到一个则进行匹配。当JsON数据中的数据结构和GO中的目标类型不匹配时,Json.Unmarshal()函数在解码过程中会丢弃该字段。

Json.Unmarshal()函数也支持读取未知的结构的Json数据,允许使用map[string]interface{}和[]interface{}类型的值来分别存放未知结构的JsON对象或数组。

package mainimport (    "enCoding/Json"	"io/IoUtil"	"net/http"	"fmt")//对应Json天气数据源的结构,头字母大写type WeatherInfoJson struct {	Pub        string	name       string	Wind       Windobject	Astronomy  AstronomyObject	Atmosphere AtmosphereObject	Forecasts  []ForecastsObject}type Windobject struct {	Chill     float64	Direction float64	Speed     float64}type AstronomyObject struct {	Sunrise string	Sunset  string}type AtmosphereObject struct {	HumIDity   float64	Visibility float64	Pressure   float64	Rising     float64}type ForecastsObject struct {	Date        string	Day         float64	Code        float64	Text        string	Low         float64	High        float64	Image_large string	Image_small string}func GetWeather(city string) {	str:="http://weather.china.xappengine.com/API?city="+city	resp,err := http.Get(str)  //使用get方法访问	if err != nil {		return	}	defer resp.Body.Close()	input,err1 := IoUtil.ReadAll(resp.Body)    //读取流数据	if err1 != nil {		return	}	var JsonWeather WeatherInfoJson	err2:=Json.Unmarshal(input,&JsonWeather)   //解析Json数据	if err2 != nil {		return	}	if len(JsonWeather.name)!=0 {   //判断有无解析数据	    for i := 0; i < 3; i++ {	        fmt.Printf("-->地区:%s 时间:%s 温度:%d-%d 天气:%s 发布时间:%s\n",JsonWeather.name,JsonWeather.Forecasts[i].Date,int(JsonWeather.Forecasts[i].Low),int(JsonWeather.Forecasts[i].High),JsonWeather.Forecasts[i].Text,JsonWeather.Pub)	    }    }}func main(){    GetWeather("深圳")    GetWeather("nanning")}

运行结果如下

总结

以上是内存溢出为你收集整理的golang读取json格式的天气预报全部内容,希望文章能够帮你解决golang读取json格式的天气预报所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1292759.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-10
下一篇2022-06-10

发表评论

登录后才能评论

评论列表(0条)

    保存