氣象局天氣預報信息接口

-------------

1 氣象局提供的接口:http://openweather.weather.com.cn/ 查看相關接口。

2  aqicn.org網站提供的數據:http://aqicn.org/publishingdata/json

---------------

可以通過訪問 http://61.4.185.48:81/g/ 獲取當前城市ID

或者訪問http://m.weather.com.cn後 網頁會自動定位當前城市 並在網址欄顯示出相應的城市ID

以南京市(id:101190101)爲例.

詳細天氣信息:http://m.weather.com.cn/atad/101190101.html

基本天氣信息:http://www.weather.com.cn/data/cityinfo/101190101.html

各種天氣指數:http://www.weather.com.cn/data/zs/101190101.html

風力方面信息:http://www.weather.com.cn/data/sk/101190101.html


以上各URL均可以直接訪問而看到相應的JSON數據。


IOS提供的最基本的JSON解析方法:NSJSONSerialization。

    // 請求
    NSURLRequest *weatherReq = [NSURLRequest requestWithURL:[NSURL URLWithString:urlLink]];
    NSData *weatherData = [NSURLConnection sendSynchronousRequest:weatherReq returningResponse:nil error:nil];
    
    // 將NSData數據存儲到NSDictionary
    NSDictionary *weatherDict = [NSJSONSerialization JSONObjectWithData:weatherData options:NSJSONReadingMutableLeaves error:&error];
    NSDictionary *weatherInfo = [weatherDict objectForKey:@"weatherinfo"];


當然如果需要提升性能,使用第三方的JSON解析方式例如JSONKit等。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章