Search code examples
iosobjective-cjsonnsdictionaryrkobjectmapping

RKObjectMapper not mapping array of dictionaries


I am using RKObjectMapping to map the response JSON to my predefined model but I am facing some issues when I try to map array of dictionaries.

JSON Response :

{
total: 274,
per_page: 10,
current_page: 1,
last_page: 28,
prev_page_url: null,
from: 1,
to: 10,
data: - [
- {
_id: "someid",
legacy_id: someid,
username: "awesome",
legacy_account_id: 12326,
brand_name: "Awesome",
last_review_ids: - [
],
category_profession_ids: - [
],
score_award: - {
},
score_award_one: - {
},
photos: - [
- {
_id: "someid"
},
- {
_id: "someanotherid"
}
}
],
enabled_at_utc: "2015-08-04 04:06:26",
published_at_utc: "2016-11-10 11:05:55",
last_activity_at_utc: "2016-04-10 16:17:06",
last_booked_at_utc: "2016-11-15 08:51:31",
last_reviewed_at_utc: "2016-11-08 10:50:13",
created_at_utc: "2015-08-04 04:03:59",
updated_at_utc: "2016-11-16 04:31:10"
},
+ {... },
+ {... },
+ {... },
+ {... },
+ {... },
+ {... },
+ {... },
+ {... },
+ {... }
],
client_version: "1"
}

And for mapping above response I have added below mapping methods

+ (RKObjectMapping *)professionalMapping{
    // setup object mappings
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ProfessionalModel class]];
    [mapping addAttributeMappingsFromArray:baseModelMapping];
    [mapping addAttributeMappingsFromArray:@[@"username", @"first_name", @"last_name", @"brand_name", @"profile_url", @"image", @"account_id", @"service_rules", @"lead_time_cancellation", @"faqs", @"work_experience", @"education", @"accolades", @"phone_no", @"contact_no", @"contact_no_hide", @"email", @"email_hide", @"website", @"website_hide", @"facebook", @"twitter", @"instagram", @"service_location", @"service_location_hide", @"housecall", @"housecall_charges", @"housecall_charges_display", @"published_at", @"enabled_at", @"created_at", @"updated_at", @"is_registered", @"id_no", @"id_full_name", @"id_front_image", @"id_back_image", @"id_address1", @"id_address2", @"id_postal_code", @"id_city", @"id_state", @"id_country", @"service_count", @"review_count", @"booking_count", @"share_url", @"distance", @"distance_unit", @"distance_display", @"neighbourhood", @"photo_count", @"review_photo_count", @"work_photo_count", @"location_photo_count", @"location_type", @"is_bookmarked", @"bookmark_id",@"user_id",@"is_followed",@"menus_display", @"payout_outstanding_display", @"payout_complete_display", @"payout_total_display", @"payout_outstanding"]];
    [mapping addAttributeMappingsFromArray:@[@"rating_overall", @"rating_accuracy", @"rating_communication", @"rating_skill", @"rating_value", @"rating_attitude", @"rating_cleanliness", @"rating_environment"]]; // Rating attr
    [mapping addAttributeMappingsFromArray:@[@"rating_overall_stats", @"rating_communication_stats", @"rating_attitude_stats", @"rating_cleanliness_stats", @"rating_environment_stats", @"rating_service_stats"]];
    [mapping addAttributeMappingsFromArray:@[@"bank_id", @"bank_account_holder_name", @"bank_account_no", @"bank_branch_code", @"bank_code"]]; // Payout Method attr
    [mapping addAttributeMappingsFromArray:@[@"transaction_fee_count_display",@"transaction_fee_count",@"currency_code",@"is_top_artist",@"top_artist_category_ids"]];
    [mapping addAttributeMappingsFromDictionary:@{@"price_new_customer_fee_display": @"price_transacton_fee_display"}];
    [mapping addAttributeMappingsFromDictionary:@{@"description": @"description2"}];
    [mapping addRelationshipMappingWithSourceKeyPath:@"user" mapping:[self accountMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"country" mapping:[self countryMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"currency" mapping:[self currencyMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"timezone" mapping:[self timezoneMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"service_address" mapping:[self addressMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"service_addresses" mapping:[self addressMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"service_address_display" mapping:[self addressMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"id_home_addr" mapping:[self addressMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"menu" mapping:[self professionMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"photos" mapping:[self photoMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"reviews" mapping:[self bookingCustomerReviewMapping]];
    [mapping addRelationshipMappingWithSourceKeyPath:@"last_reviews" mapping:[self bookingCustomerReviewMapping]];//because account (?)
    return mapping;
}



+ (RKObjectMapping *)professionalTotalMapping{

    RKObjectMapping* professionalMapping = [self professionalMapping];

    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ProfessionalTotalMapping class]];
    [mapping addAttributeMappingsFromArray:@[@"total"]];

    [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"data" toKeyPath:@"professional" withMapping:professionalMapping]];

    return mapping;
}

But all I get is nil initialised ProfessionalTotalMapping and the code can't even map a single key.

Please let me know whats wrong in my mapping code.

Any help would be much appreciated.

Thanks


Solution

  • Found the solution by not creating 2 separate models. So now I am parsing the whole response including all those required fields and creating a single model object which has all the data that I need.

    Thanks