0

我已经浏览了包括https://github.com/RestKit/RestKit/wiki/Object-mapping在内的可用文档,但我无法找到将 GET 结果映射到的解释或示例

  • (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects

(NSArray*)objects 到底是什么?

当我从服务器只返回一个 JSON 对象时,我能够将其映射为:

NSString *bodyResults;
bodyResults = [[objectLoader response] bodyAsString];

NSDictionary *resultsDictionary = [bodyResults objectFromJSONString];

NSString *subject = [resultsDictionary  objectForKey:@"subject"];

但是现在我要返回一个 JSON 列表(一组对象),我遇到了麻烦。

我在提交获取之前映射了对象:

//地图提供 RKObjectMapping* OfferingMapping = [RKObjectMapping mappingForClass:[Offering class]]; [offeringMapping mapKeyPath:@"categoryId" toAttribute:@"categoryId"]; [offeringMapping mapKeyPath:@"merchantId" toAttribute:@"merchantId"]; [offeringMapping mapKeyPath:@"name" toAttribute:@"name"]; [offeringMapping mapKeyPath:@"latitude" toAttribute:@"latitude"]; [offeringMapping mapKeyPath:@"longitude" toAttribute:@"longitude"];

[[RKObjectManager sharedManager].mappingProvider setMapping:offeringMapping forKeyPath:@"offering"];

double latitude = [(AppDelegate*)[[UIApplication sharedApplication] delegate] currentLatitude];
double longitude = [(AppDelegate*)[[UIApplication sharedApplication] delegate] currentLongitude];


NSString *latitudeString = [[NSNumber numberWithDouble:latitude] stringValue];
NSString *longitudeString = [[NSNumber numberWithDouble:longitude] stringValue];

NSDictionary *getParams = [NSDictionary dictionaryWithKeysAndObjects:
                            @"lat",latitudeString,
                            @"long",longitudeString,
                            nil];

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/merchantofferings" stringByAppendingQueryParameters:getParams] delegate:self];

谢谢

4

1 回答 1

0

在回答您的第一个问题时, (NSArray *)objects 是由 objectLoader 加载的对象的数组。因此,在您的情况下,它们应该是产品类型的对象,其中填充了您在对象映射中映射的属性。

您是否有从 GET 请求返回的 JSON 示例,可以在此处发布?您确定它是有效的 JSON 并且完全按照您的预期返回吗?

于 2012-09-26T23:24:02.890 回答