2012-11-07 154 views
0

我想使用RKObjectMapping执行对象映射。使用RKObjectMapping嵌套映射

这里是我的JSON的示例:

{ 
     "notification": { 
      "created_at": "2012-10-23T02:43:43-04:00", 
      "delivered": true, 
      "id": 3915, 
      "missed": false, 
      "rejected": false, 
      "search_request_id": 360, 
      "bid": { 
       "bid_type": "none", 
       "bid_type_values": null, 
       "comment": "hi\n", 
       "created_at": "2012-10-23T02:43:54-04:00", 
       "delivered": true, 
       "delivery_type": "email", 
       "id": 268, 
         "has_chat_session": true, 
      }, 
      "search_request": { 
       "created_at": "2012-10-23T02:43:42-04:00", 
       "customer_id": 6, 
       "id": 360, 
       "latitude": "40.7536854", 
       "location": "10001", 
       "longitude": "-73.9991637", 
      } 
     } 

我已经使用jsonlint验证了JSON,它是有效的。

这是我的映射。

RKObjectMapping *bidMapping = [RKObjectMapping mappingForClass:[Bid class]]; 
    [bidMapping mapKeyPath:@"comment" toAttribute:@"comment"]; 
    [bidMapping mapKeyPath:@"chat_session_id" toAttribute:@"chatSessionId"]; 
    [objectManager.mappingProvider setMapping:bidMapping forKeyPath:@"bid"]; 

    RKObjectMapping *searchRequestMapping = [RKObjectMapping mappingForClass:[SearchRequest class]]; 
    [searchRequestMapping mapKeyPath:@"purpose" toAttribute:@"purpose"]; 
    [searchRequestMapping mapKeyPath:@"customer_Id" toAttribute:@"customerId"]; 
    [searchRequestMapping mapKeyPath:@"need" toAttribute:@"need"]; 
    [objectManager.mappingProvider setMapping:searchRequestMapping forKeyPath:@"search_request"]; 

    RKObjectMapping *notificationMapping = [RKObjectMapping mappingForClass:[Notification class]]; // have not mapped all the attributes. 
    [notificationMapping mapKeyPath:@"id" toAttribute: @"notificationId"]; 
    [notificationMapping mapKeyPath:@"created_at" toAttribute:@"timestamp"]; 
    [notificationMapping mapKeyPath:@"vendor_id" toAttribute:@"vendorId"]; 
    [notificationMapping mapKeyPath:@"bid" toRelationship:@"bid" withMapping:bidMapping]; 
    [notificationMapping mapKeyPath:@"search_request" toRelationship:@"search_request" withMapping:notificationMapping]; 
    [objectManager.mappingProvider setMapping:notificationMapping forKeyPath:@"notification"]; 

该程序崩溃与断言失败,我无法调试时,我尝试loadObjectsAtResourcePath。我也尝试使用rootKeyPaths,但无济于事。

有人可以帮我找到错误吗?

谢谢!

ps:我想添加一个NSDictionary参数,文档说使用appendQueryParams:params。但我收到警告说它已被弃用。我可以使用任何替代方案?

回答

1

你给错了测绘这里 -

[notificationMapping mapKeyPath:@"search_request" toRelationship:@"search_request" withMapping:notificationMapping]; 

您chould给searchRequestMapping喜欢 -

[notificationMapping mapKeyPath:@"search_request" toRelationship:@"search_request" withMapping:searchRequestMapping]; 

相反appendQueryParams的:PARAMS,你可以用下面的方法的RKClient-

- (RKRequest *)get:(NSString *)resourcePath queryParameters:(NSDictionary *)queryParameters delegate:(NSObject<RKRequestDelegate> *)delegate; 
方法
+0

facepalm。我真的很愚蠢的错误。感谢你看到Rahul。干杯! – Sohan

+0

答案已过时 –

+0

嗨,Alberto,你可以在这里更新/发布你的答案吗?几年前我已停止在iOS上工作,所以我不知道最近的变化。 –