This is a creation in Article, where the information may have evolved or changed.
interface for using weather forecasts: http://weather.china.xappengine.com/api?city= Nanning (looking for a long time to find a support pinyin, but it seems the small place is no data)
Access to data compressed in JSON format, after formatting
{"Pub": "2013-06-29 22:59", "name": "Nanning", "Wind": {"Chill": Bayi, "direction": "," "Speed": 7}, "Astronomy": {"Sunrise": "6:05", "Sunset": "19:34"}, "atmosphere": {"humidity": "Visibility": 6.21, "pressure": 29.71, "Rising": 1}, "forecasts": [{"D Ate ":" 2013-06-29 "," Day ": 6," code ": +," text ":" Partly cloudy "," Low ": 26, "High": +, "Image_large": "Http://weather.china.xappengine.com/static/w/img/d29.png", "Image_smal L ":" Http://weather.china.xappengine.com/static/w/img/s29.png "}, {" Date ":" 2013-06-30 ", "Day": 0, "code": +, "text": "Partly cloudy", "low": +, "high": "I Mage_large ":" Http://weather.china.xappengine.com/static/w/img/d30.png "," Image_small ":" Http://weather.china.x appengine.com/static/w/iMg/s30.png "}, {" Date ":" 2013-07-01 "," Day ": 1," code ": PNs," Te XT ": Partial Thunderstorm", "low": +, "high": +, "Image_large": "http://weather.china.xappengine.com/s Tatic/w/img/d37.png "," Image_small ":" Http://weather.china.xappengine.com/static/w/img/s37.png "}, {"Date": "2013-07-02", "Day": 2, "code": $, "text": "Sporadic Thunderstorm", "Lo W ": +," high ": +," 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": "201 3-07-03 "," Day ": 3," code ": $," text ":" Sporadic Thunderstorm "," low ": +," high ": "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 language built-in Encoding/json standard library, Json.unmarshal () can parse Json data
Convention: A Boolean value in JSON resolves to a Boolean value, a number (integer, float) resolves to float64,string to parse to a string, the array resolves to an interface array, and null resolves to nil. These fields must be in the type declaration that begin with an uppercase letter and can be exported.
The Json.unmarshal () function finds the fields in the target structure in the order of a contract and matches if one is found. JSON when the data structure in the JSON data does not match the target type in go. The Unmarshal () function discards the field during decoding.
The Json.unmarshal () function also supports reading JSON data for unknown structures, allowing the use of values of map[string]interface{} and []interface{} types to separate JSON objects or arrays of unknown structures.
Package Mainimport ("Encoding/json" "Io/ioutil" "Net/http" "FMT")//corresponds to the structure of the JSON weather data source, with the first letter capitalized type Weatherinfojson struct { Pub stringname stringwind windobjectastronomy astronomyobjectatmosphere atmosphereobjectforecasts []F Orecastsobject}type windobject struct {Chill float64direction float64speed float64}type astronomyobject struct {Su Nrise 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.x Appengine.com/api?city= "+cityresp, err: = http. Get (str)//Use the Get method to access the IF err! = nil {Return}defer resp. Body.close () input, err1: = Ioutil. ReadAll (resp. Body)//Read stream data if err1! = nil {Return}var jsonweather Weatherinfojsonerr2:=json. Unmarshal (input, &jsonweather)//parsing JSOn data if err2! = nil {return}if len (jsonweather.name)!=0 {///judgment has no parse data for I: = 0; i < 3; i++ {fmt. Printf ("--area:%s Time:%s Temp:%d-%d weather:%s Published:%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 ("Shenzhen") GetWeather ("Nanning")}
The operation results are as follows