RKObjectMapper 不映射字典数组
RKObjectMapper not mapping array of dictionaries
我正在使用 RKObjectMapping
将响应 JSON
映射到我的预定义模型,但是当我尝试映射字典数组时遇到一些问题。
JSON 响应:
{
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"
}
为了映射上面的响应,我添加了下面的映射方法
+ (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;
}
但我得到的只是 nil 初始化 ProfessionalTotalMapping
并且代码甚至无法映射单个键。
请让我知道我的映射代码有什么问题。
如有任何帮助,我们将不胜感激。
谢谢
通过不创建 2 个单独的模型找到了解决方案。
所以现在我正在解析整个响应,包括所有这些必填字段,并创建一个包含我需要的所有数据的模型对象。
谢谢
我正在使用 RKObjectMapping
将响应 JSON
映射到我的预定义模型,但是当我尝试映射字典数组时遇到一些问题。
JSON 响应:
{
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"
}
为了映射上面的响应,我添加了下面的映射方法
+ (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;
}
但我得到的只是 nil 初始化 ProfessionalTotalMapping
并且代码甚至无法映射单个键。
请让我知道我的映射代码有什么问题。
如有任何帮助,我们将不胜感激。
谢谢
通过不创建 2 个单独的模型找到了解决方案。 所以现在我正在解析整个响应,包括所有这些必填字段,并创建一个包含我需要的所有数据的模型对象。
谢谢