【问题标题】:AlamofireObjectMapper: JSON with IdentifierAlamofireObjectMapper:带有标识符的 JSON
【发布时间】:2015-11-02 15:27:45
【问题描述】:

封装数据时是否需要为AlamofireObjectMapper写一个wrapper-object?例如。如果天气数据位于命名数组“data”中。映射的最佳解决方案是什么?

{
    "data": [
        {
            "conditions": "Partly cloudy",
            "day": "Monday",
            "temperature": 20
        },
        {
            "conditions": "Showers",
            "day": "Tuesday",
            "temperature": 22
        },
        {
            "conditions": "Sunny",
            "day": "Wednesday",
            "temperature": 28
        }
    ]
}

let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json"
Alamofire.request(.GET, URL, parameters: nil)
         .responseArray { (response: [Forecast]?, error: ErrorType?) in
            if let response = response {
                for forecast in response {
                    print(forecast.day)
                    print(forecast.temperature)           
                }
            }
}

【问题讨论】:

标签: swift alamofire


【解决方案1】:

其实这是最舒服的选择。

你可以有一个像这样的包装对象:

class Forecast: Mappable {

   // MARK: - Attributes

   var conditions: String = ""
   var day: String = ""
   var temperature: NSNumber

   // MARK: - Methods

   init() { }

   required init?(_ map: Map) { }

   func mapping(map: Map) {
       conditions <- map["conditions"]
       day <- map["day"]
       temperature <- map["temperature"]
   }
} 

然后,管理请求:

Alamofire.request(.POST, URL).responseObject({ (response: Response<Forecast, NSError>) -> Void in
            if response.result.isSuccess {
                print(response.result.value!)
            }
        })

祝你好运!

【讨论】:

    【解决方案2】:

    您可以使用 AnyObject 映射响应,如下例所示;

        let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json"
        Alamofire.request(.GET, URL).responseJSON { (response : Response<AnyObject, NSError>) -> Void in
            print(response.result.value.debugDescription)
            let arr = response.result.value! as? Array<AnyObject>
    
            for a in arr! {
                let condition = a["conditions"]
                let day = a["day"]
                print("\(day!!) condition is \(condition!!)")
            }
    
    
        }
    

    输出:

    Monday condition is Partly cloudy
    Tuesday condition is Showers
    Wednesday condition is Sunny 
    

    【讨论】:

      【解决方案3】:

      所以查看文档,看起来数据是在请求级别指定的,如他们在此处的示例中所示:

      let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json"
      Alamofire.request(.GET, URL).responseObject("data") { (response: 
      Response<WeatherResponse, NSError>) in
      
      let weatherResponse = response.result.value
      
      print(weatherResponse?.location)
      if let threeDayForecast = weatherResponse?.threeDayForecast {
          for forecast in threeDayForecast {
              print(forecast.day)
              print(forecast.temperature)           
           }
        }
      }
      

      关键是:

      Alamofire.request(.GET, URL).responseObject("data") {(响应:

      【讨论】:

      • 他们用 AOM 2.0 改变了他们的 API。你的意思是使用 .responseObject(, completionHandler: -> Void#>)
      猜你喜欢
      • 2021-08-02
      • 2013-10-18
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多