Overcoat - displays several types of server responses

I can't figure out how to map the response array when the JSON response has different envelope keys:

EXAMPLE Answers:

 "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
      },

      

I created a ServerResponse object:

@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]
             };
}

      

This causes the ONE SPCCountry object to be created with null data. I want a LIST of SPCCountry / SCCurrency objects.

ANSWER: For each unique response path, you need to subclass OCVResponse and return the class and path to the class method:

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

}

      

Response classes should only be implemented:

@implementation SPCCountryResponse

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

@end

      

+3


source to share





All Articles