golang讀取json格式的天氣預報

使用天氣預報的接口:http://weather.china.xappengine.com/api?city=南寧 (找了很久才找到一個支持拼音的,不過好像小地方是沒數據的) json

訪問獲得json格式壓縮的數據,格式化後 api


{
    "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對象或數組。 app

package main

import (
    "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")

}

運行結果以下 函數

相關文章
相關標籤/搜索