【问题标题】:How to get data from JSON response using Alamofire request如何使用 Alamofire 请求从 JSON 响应中获取数据
【发布时间】:2016-03-23 22:31:05
【问题描述】:

API 会返回下一个:

{
    apparentTemperature = "37.08";
    cloudCover = 1;
    dewPoint = "31.44";
    humidity = "0.8";
    icon = cloudy;
    ozone = "305.37";
    precipIntensity = "0.0023";
    precipProbability = "0.06";
    precipType = rain;
    pressure = "1029.17";
    summary = Overcast;
    temperature = "37.08";
    time = 1450515600;
    windBearing = 196;
    windSpeed = "2.61";
},
{
    apparentTemperature = "39.22";
    cloudCover = 1;
    dewPoint = "33.13";
    humidity = "0.79";
    icon = cloudy;
    ozone = "304.86";
    precipIntensity = "0.0016";
    precipProbability = "0.03";
    precipType = rain;
    pressure = "1029.13";
    summary = Overcast;
    temperature = "39.22";
    time = 1450519200;
    windBearing = 203;
    windSpeed = "2.76";
},
{
    apparentTemperature = "41.17";
    cloudCover = 1;
    dewPoint = "34.77";
    humidity = "0.78";
    icon = cloudy;
    ozone = "303.99";
    precipIntensity = "0.0015";
    precipProbability = "0.03";
    precipType = rain;
    pressure = "1028.96";
    summary = Overcast;
    temperature = "41.17";
    time = 1450522800;
    windBearing = 213;
    windSpeed = "2.73";
},
{
    apparentTemperature = "42.47";
    cloudCover = "0.99";
    dewPoint = "36.04";
    humidity = "0.78";
    icon = cloudy;
    ozone = "302.97";
    precipIntensity = "0.0013";
    precipProbability = "0.02";
    precipType = rain;
    pressure = "1028.82";
    summary = Overcast;
    temperature = "42.47";
    time = 1450526400;
    windBearing = 215;
    windSpeed = "2.58";
});
    icon = "partly-cloudy-day";
    summary = "Mostly cloudy starting tomorrow morning.";
};
    latitude = "45.745138";
    longitude = "21.169898";
    offset = 2;
    timezone = "Europe/Bucharest";
})

我的代码是:

import UIKit
import Alamofire

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var textView: UITextView!

    @IBAction func searchWeather(sender: UIButton) {
        loadData()

    }

    func loadData() {
        let city: String = textField.text!
        let api_key: String = "https://api.forecast.io/forecast/b33607b3d2611f604df5def613283a8a/"
        Alamofire.request(.GET, "\(api_key)45.745138,21.169898").responseJSON { response in
            print("\(response.result.value)")

        }

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

我想从 JSON 响应中获取一些字段,如温度、湿度和压力,并将它们放在 UItextView 到我的应用程序中。我怎样才能只得到三个字段?

【问题讨论】:

标签: swift


【解决方案1】:

这里是湿度示例(将“湿度”替换为“温度”和“压力”。另请参阅我如何进入您的 JSON,使用此示例了解如何导航并获得您想要的内容

let daily = response.result.value!["daily"] as! NSDictionary
let data = daily["data"] as! NSArray
for obj in data {
  print (obj["humidity"])
}

【讨论】:

  • 谢谢,你能解释一下感叹号在一般情况下和 as 之后的具体含义吗
  • 堆栈上已经有一个关于它的问题和答案:) *.com/questions/24018327/…
【解决方案2】:

继续@hla 的回答,应尽可能避免强制展开/强制转换。如果值为 nil/无法转换,它们将导致您的应用程序崩溃。这种东西请使用optional binding

正确的实现应该是:

if let daily = response.result.value?["daily"] as? NSDictionary, let data = daily["data"] as? NSArray {
    for obj in data {
        print (obj["humidity"])
    }
}

【讨论】: