博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
golang读取json格式的天气预报
阅读量:6856 次
发布时间:2019-06-26

本文共 3962 字,大约阅读时间需要 13 分钟。

  hot3.png

使用天气预报的接口: (找了好久才找到一个支持拼音的,不过好像小地方是没数据的)

访问得到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,            "text": "局部多云",            "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,            "high": 32,            "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": "零星雷雨",            "low": 25,            "high": 32,            "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,            "code": 38,            "text": "零星雷雨",            "low": 25,            "high": 32,            "image_large": "http://weather.china.xappengine.com/static/w/img/d38.png",            "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")}

运行结果如下

转载于:https://my.oschina.net/mocos/blog/141336

你可能感兴趣的文章
2019最新大厂面试算法真题解析
查看>>
深入理解js闭包
查看>>
还有什么等待:一对一直播开发+一对多,跨入全能APP时代
查看>>
发布项目到 jitpack io
查看>>
nextTick 在 vue 2.5 和 vue 2.6 之间有什么不同
查看>>
(十五)spring cloud微服务分布式云架构-commonservice-config配置服务搭建
查看>>
splice 循环删除元素
查看>>
vue单元测试vue test utils使用初探
查看>>
看promise教你如何优雅的写js异步代码
查看>>
Laravel——语言包
查看>>
Swift可选链式调用
查看>>
java B2B2C Springcloud电子商务平台源码-zuul 过滤器机制
查看>>
【更新】LEADTOOLS v20最新版发布(三)
查看>>
企业分布式微服务云SpringCloud SpringBoot mybatis -服务的注册与发现(Eureka)
查看>>
java B2B2C 仿淘宝电子商城系统-Zuul的使用
查看>>
深度解析数据分析、大数据工程师和数据科学家的区别
查看>>
Linux-文件处理命令-file
查看>>
关于一个小程序
查看>>
利用dispatch_once创建单例
查看>>
Centos LVS DR模式详细搭建过程
查看>>