3

当响应 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 对象的列表。

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

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

}

响应类的实现只需要实现:

@implementation SPCCountryResponse

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

@end
4

0 回答 0