1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import
UIKit
class
ViewController
:
UIViewController
{
let
apiId =
"12b2817fbec86915a6e9b4dbbd3d9036"
override
func
viewDidLoad() {
super
.viewDidLoad()
getCurrentWeatherData();
}
//获取当前天气数据(北京)
func
getCurrentWeatherData(){
let
urlStr =
"http://api.openweathermap.org/data/2.5/weather?q=beijing&units=metric&appid=\(apiId)"
let
url =
NSURL
(string: urlStr)!
guard
let
weatherData =
NSData
(contentsOfURL: url)
else
{
return
}
//将获取到的数据转为json对象
let
jsonData =
JSON
(data: weatherData)
//日期格式化输出
let
dformatter =
NSDateFormatter
()
dformatter.dateFormat =
"yyyy年MM月dd日 HH:mm:ss"
print
(
"城市:\(jsonData["
name
"].string!)"
)
let
weather = jsonData[
"weather"
][0][
"main"
].string!
print
(
"天气:\(weather)"
)
let
weatherDes = jsonData[
"weather"
][0][
"description"
].string!
print
(
"详细天气:\(weatherDes)"
)
let
temp = jsonData[
"main"
][
"temp"
].number!
print
(
"温度:\(temp)°C"
)
let
humidity = jsonData[
"main"
][
"humidity"
].number!
print
(
"湿度:\(humidity)%"
)
let
pressure = jsonData[
"main"
][
"pressure"
].number!
print
(
"气压:\(pressure)hpa"
)
let
windSpeed = jsonData[
"wind"
][
"speed"
].number!
print
(
"风速:\(windSpeed)m/s"
)
let
lon = jsonData[
"coord"
][
"lon"
].number!
let
lat = jsonData[
"coord"
][
"lat"
].number!
print
(
"坐标:[\(lon),\(lat)]"
)
let
timeInterval1 =
NSTimeInterval
(jsonData[
"sys"
][
"sunrise"
].number!)
let
date1 =
NSDate
(timeIntervalSince1970: timeInterval1)
print
(
"日出时间:\(dformatter.stringFromDate(date1))"
)
let
timeInterval2 =
NSTimeInterval
(jsonData[
"sys"
][
"sunset"
].number!)
let
date2 =
NSDate
(timeIntervalSince1970: timeInterval2)
print
(
"日落时间:\(dformatter.stringFromDate(date2))"
)
let
timeInterval3 =
NSTimeInterval
(jsonData[
"dt"
].number!)
let
date3 =
NSDate
(timeIntervalSince1970: timeInterval3)
print
(
"数据时间:\(dformatter.stringFromDate(date3))"
)
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import
UIKit
class
ViewController
:
UIViewController
{
let
apiId =
"12b2817fbec86915a6e9b4dbbd3d9036"
override
func
viewDidLoad() {
super
.viewDidLoad()
getForcastWeatherData();
}
//获取未来天气数据(北京)
func
getForcastWeatherData(){
let
urlStr =
"http://api.openweathermap.org/data/2.5/forecast?q=beijing&units=metric&appid=\(apiId)"
let
url =
NSURL
(string: urlStr)!
guard
let
weatherData =
NSData
(contentsOfURL: url)
else
{
return
}
//将获取到的数据转为json对象
let
weatherJson =
JSON
(data: weatherData)
print
(
"城市:\(weatherJson["
city
"]["
name
"].string!)"
)
let
lon = weatherJson[
"city"
][
"coord"
][
"lon"
].number!
let
lat = weatherJson[
"city"
][
"coord"
][
"lat"
].number!
print
(
"坐标:[\(lon),\(lat)]"
)
//日期格式化输出
let
dformatter =
NSDateFormatter
()
dformatter.dateFormat =
"yyyy年MM月dd日 HH:mm:ss"
//遍历数据
for
(_,jsonData):(
String
,
JSON
)
in
weatherJson[
"list"
] {
let
timeInterval =
NSTimeInterval
(jsonData[
"dt"
].number!)
let
date =
NSDate
(timeIntervalSince1970: timeInterval)
print
(
"--- 时间:\(dformatter.stringFromDate(date)) ---"
)
let
weather = jsonData[
"weather"
][0][
"main"
].string!
print
(
"天气:\(weather)"
)
let
weatherDes = jsonData[
"weather"
][0][
"description"
].string!
print
(
"详细天气:\(weatherDes)"
)
let
temp = jsonData[
"main"
][
"temp"
].number!
print
(
"温度:\(temp)°C"
)
let
humidity = jsonData[
"main"
][
"humidity"
].number!
print
(
"湿度:\(humidity)%"
)
let
pressure = jsonData[
"main"
][
"pressure"
].number!
print
(
"气压:\(pressure)hpa"
)
let
windSpeed = jsonData[
"wind"
][
"speed"
].number!
print
(
"风速:\(windSpeed)m/s"
)
}
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|