将Alamofire结果保存为variables?

我一直在试图找出如何将Alamofire的JSON响应的一部分保存为一个variables来做一个if语句,似乎无法find如何去做。

我做了一段代码来作为由以下内容组成的问题的例子:

import UIKit import Alamofire class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() Alamofire.request("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44143256ee15e9c3f521ca062463dd8d").responseJSON { response in print(response.result) // result of response serialization if let JSON = response.result.value { print("JSON: \(JSON)") } } } } 

我从API请求中获得以下响应:

 JSON: { base = stations; clouds = { all = 0; }; cod = 200; coord = { lat = "51.51"; lon = "-0.13"; }; dt = 1485031800; id = 2643743; main = { humidity = 83; pressure = 1026; temp = "270.54"; "temp_max" = "273.15"; "temp_min" = "267.15"; }; name = London; sys = { country = GB; id = 5091; message = "0.0038"; sunrise = 1484985141; sunset = 1485016345; type = 1; }; visibility = 7000; weather = ( { description = haze; icon = 50n; id = 721; main = Haze; } ); wind = { speed = 1; }; } 

从这个JSON响应我想保存天气说明,所以我可以使用下面的语句:

 if var currentWeather = sunny { return "Nice day!" } else { return "Uh-Oh, keep warm!" } 

如果有人能帮助我,我将不胜感激! 如果我不得不使用SwiftyJSON来使它更容易,那就好了,我只是一直在挣扎,需要弄清楚它!

您可以在打印出您的JSON响应之后分析您的结果:

 guard let JSON = response.result.value as? [String:Any], let weather = JSON["weather"] as? [[String:Any]] else { print("Could not parse weather values") return } for element in weather { if let description = element["description"] as? String { print(description) } } 

如果你想保存结果,或者根据你所描述的某个结果做一些事情,你可以插入别的东西,而不仅仅是print(description)如:

 if description == "sunny" { //do something } 

让我知道这是否有道理。 请记住, ()在Xcode控制台意味着“arrays”。