這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
使用天氣預報的介面: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, "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 stringName stringWind WindObjectAstronomy AstronomyObjectAtmosphere AtmosphereObjectForecasts []ForecastsObject}type WindObject struct {Chill float64Direction float64Speed float64}type AstronomyObject struct {Sunrise stringSunset string}type AtmosphereObject struct {Humidity float64Visibility float64Pressure float64Rising float64}type ForecastsObject struct {Date stringDay float64Code float64Text stringLow float64High float64Image_large stringImage_small string}func GetWeather(city string) {str:="http://weather.china.xappengine.com/api?city="+cityresp, 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 WeatherInfoJsonerr2:=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")}
運行結果如下