1

我想第一次使用 AlamofireObjectMapper 来快速解析 json 响应。

回应是:

{
    "city": {
        "total": 19,
        "data": [
            {
                "business_model": 2,
                "type": "Hotel"
            },
            {
                "business_model": 2,
                "type": "Hotel"
            }
        ]
    }
}

我像这样映射它:

class CityResponse: Mappable {

    var location: String?
    var data: [Data]?

    required init?(_ map: Map){

    }

    func mapping(map: Map) {
        data <- map["city.data"]

    }
}

class Data: Mappable {

    var business_model: String?
    required init?(_ map: Map){

    }

    func mapping(map: Map) {
        business_model <- map["business_model"]
    }
}

在我的控制器中:

Alamofire.request(.GET, "MY_URL").responseArray { (response: Response<[Data], NSError>) in

    print("res: \(response)")

    let forecastArray = response.result.value

    if let forecastArray = forecastArray {
        for forecast in forecastArray {
            print("model is : \(forecast.business_model)" )
        }
    }
}

它给了我错误映射。

4

1 回答 1

0

您需要指出数组的路径: responseArray("data")

Alamofire.request(.GET, "MY_URL").responseArray("data") { (response: Response<[Data], NSError>) in

    print("res: \(response)")

    let forecastArray = response.result.value

    if let forecastArray = forecastArray {
        for forecast in forecastArray {
            print("model is : \(forecast.business_model)" )
        }
    }
}
于 2016-03-04T22:44:12.407 回答