我在解码JSON
.Trying以获得日出和日落时间时遇到了麻烦,但什么都不起作用。帮助
func getData(latitude: String,longtitude: String) {
let url = URL(string: "https://api.sunrise-sunset.org/json?lat=\(latitude)&lng=\(longtitude)")
Alamofire.request(url!).responseJSON { (response) in
print(response)
guard let data = response.data else { return }
do {
let st = try JSONDecoder().decode(results.self, from: data)
print(st.sunrise)
}
catch {
print("error")
}
}
}
发布于 2018-07-09 21:08:32
您需要的也是一个根结构来解码,如下所示
struct Root : Decodable {
let status: String
let results: results
}
struct results: Decodable {
let sunrise : string
//other struc proporties as well in here
let astronomical_twilight_end: String
}
所以你可以这样做
let root = try JSONDecoder().decode(Root.self, from: data)
让result = root.result
https://stackoverflow.com/questions/48512332
复制相似问题