大衣 - 映射多种类型的服务器响应

时间:2014-10-30 21:08:58

标签: ios afnetworking overcoat

当响应JSON具有不同的信封密钥时,我无法弄清楚如何映射响应数组:

示例回应:

 "response": {
    "currencies": [
      {
        "id": 5,
        "name": "British Pound",
        "shortName": "GBP",
        "symbol": "£",
        "symbolPreceedsValue": true
      },
      {
        "id": 6,
        "name": "U.S. Dollar",
        "shortName": "USD",
        "symbol": "$",
        "symbolPreceedsValue": true
      },

{
  "response": {
    "countries": [
      {
        "id": 9,
        "name": "United Kingdom",
        "shortName": "GB",
        "isMajorMarket": true
      },
      {
        "id": 10,
        "name": "USA",
        "shortName": "US",
        "isMajorMarket": true
      },

我已经设置了一个ServerResponse对象:

@implementation ServerResponse

+(NSString *)resultKeyPathForJSONDictionary:(NSDictionary *)JSONDictionary
{
    return @"response";
}

@end

// And setup what I believe to map the response for the Country
+ (NSDictionary *)responseClassesByResourcePath {
    return @{
             @"countries": [SPCCountry class]
             };
}

它导致使用空数据创建一个SPCCountry对象。我想要一个SPCCountry / SCCurrency对象的列表。

解答: 对于每个唯一的响应路径,您需要创建一个OCVResponse子类并返回类方法中的类和路径:

+ (NSDictionary *)responseClassesByResourcePath {
    return @{
             @"/install": [ServerResponse class],
             @"/countries": [SPCCountryResponse class],
             @"/currencies": [SPCCurrencyResponse class]
             };

}

响应类的实现只需实现:

@implementation SPCCountryResponse

    +(NSString *)resultKeyPathForJSONDictionary:(NSDictionary *)JSONDictionary
    {
        return @"response.countries";
    }

@end

0 个答案:

没有答案