0

我正在尝试使用 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。但我收到警告说它已被弃用。我可以使用任何替代品吗?

4

1 回答 1

1

您在这里给出了错误的映射-

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

你应该给 searchRequestMapping 喜欢-

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

代替 appendQueryParams:params,您可以使用 RKClient 的方法以下方法-

- (RKRequest *)get:(NSString *)resourcePath queryParameters:(NSDictionary *)queryParameters delegate:(NSObject<RKRequestDelegate> *)delegate;
于 2012-11-07T07:49:07.407 回答