我正在尝试映射此JSON数据:
{
"Id": 1,
"Question": [
{
"Id": 1,
"PicUrl": "sample string 2",
"CorrectAnswer": "sample string 3",
"Difficulty": 4,
"CategoryId": 5,
"CategoryName": "sample string 6",
"AccountId": 7
},
{
"CorrectAnswer": "sample string 3",
"Difficulty": 4,
"CategoryId": 5,
"CategoryName": "sample string 6",
"AccountId": 7
},
{
"StartTime": "2013-10-09T00:54:46.5522592+00:00"
}
但我遇到了一些问题。在经过object mapping overview of Restkit一遍又一遍之后,我将我的数据分成3个不同的类。 CurrentGames,项目和答案。我将答案类映射为here中描述的无关键路径,并且对于我创建了与答案类的关系的项目类。我创建了从items到currentgames类的另一个关系。我相信我的映射部分是正确的。但我在代码中遗漏了一些东西,因为我没有正确获取数据。我一直在尝试将其配置一周。如果有人可以提供帮助,我将非常感谢大家!
这就是我的控制台的样子:
2013-10-08 20:24:38.366 FlickRest[5126:a0b] I restkit:RKLog.m:34 RestKit logging initialized...
2013-10-08 20:24:38.518 FlickRest[5126:a0b] answers mapping <RKObjectMapping:0x8e59110 objectClass=answers propertyMappings=(
"<RKAttributeMapping: 0x8e576f0 (null) => answerss>"
)>
2013-10-08 20:24:38.519 FlickRest[5126:a0b] items mapping <RKObjectMapping:0x8e57aa0 objectClass=items propertyMappings=(
"<RKAttributeMapping: 0x8e30cd0 CategoryId => CategoryId>",
"<RKAttributeMapping: 0x8e56560 AccountId => accountId>",
"<RKAttributeMapping: 0x8e56580 Id => ID>",
"<RKAttributeMapping: 0x8e552a0 CategoryName => CategoryName>",
"<RKAttributeMapping: 0x8e552e0 Difficulty => Difficulty>",
"<RKAttributeMapping: 0x8e587c0 CorrectAnswer => correctAnswer>",
"<RKAttributeMapping: 0x8e55400 PicUrl => PicURL>",
"<RKRelationshipMapping: 0x8e567c0 Answers => Answers>"
)>
2013-10-08 20:24:38.520 FlickRest[5126:a0b] currentGames mapping <RKObjectMapping:0x8e569a0 objectClass=CurrentGames propertyMappings=(
"<RKAttributeMapping: 0x8e56a00 StartTime => StartTime>",
"<RKAttributeMapping: 0x8e56a20 GameId => GameId>",
"<RKRelationshipMapping: 0x8e595b0 Items => Items>"
)>
2013-10-08 20:24:38.549 FlickRest[5126:a0b] I restkit.network:RKObjectRequestOperation.m:180 GET 'https://xxx.net'
2013-10-08 20:24:39.287 FlickRest[5126:3507] I restkit.network:RKObjectRequestOperation.m:250 GET 'xxx.net' (200 OK / 1 objects) [request=0.7201s mapping=0.0172s total=0.7687s]
2013-10-08 20:24:39.287 FlickRest[5126:a0b] I app:WelcomeViewController.m:91 Load collection of Articles: (
"<CurrentGames: 0x8b90d70>"
)
我的班级:
answers.h
#import <Foundation/Foundation.h>
@interface answers : NSObject
@property (nonatomic,copy) NSString * answerss;
@end
items.h
#import <Foundation/Foundation.h>
#import "answers.h"
@interface items : NSObject
@property (nonatomic)NSNumber *ID;
@property(nonatomic,copy)NSURL *PicURL;
@property (nonatomic,copy)NSString *correctAnswer;
@property (nonatomic)NSNumber *Difficulty;
@property(nonatomic) NSNumber *CategoryId;
@property(nonatomic,copy)NSString *CategoryName;
@property(nonatomic)NSNumber *accountId;
@end
CurrentGames.h
#import <Foundation/Foundation.h>
#import "items.h"
@interface CurrentGames : NSObject
@property (nonatomic,copy)NSNumber *GameId;
@property (nonatomic) items *Items;
@property (nonatomic,copy) NSDate *StartTime;
@end
查看我正在使用的控制器:
WelcomeViewController.m
@implementation WelcomeViewController
-(void)loadGame
{
// Mapping for the answers class. Mapped as an array with no key-path
RKObjectMapping *answersMapping = [RKObjectMapping mappingForClass:[answers class]];
[answersMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"answerss"]];
// Mapping for the items class. Mapped as relation to answers class
RKObjectMapping *itemsMapping =[RKObjectMapping mappingForClass:[items class]];
[itemsMapping addAttributeMappingsFromDictionary:@{@"Id":@"ID",
@"PicUrl":@"PicURL",
@"CorrectAnswer":@"correctAnswer",
@"Difficulty":@"Difficulty",
@"CategoryId":@"CategoryId",
@"CategoryName":@"CategoryName",
@"AccountId":@"accountId"
}];
[itemsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Answers" toKeyPath:@"Answers" withMapping:answersMapping]];
//Mapping for the CurrentGames class. Mapped as relation to items class.
RKObjectMapping *CurrentGamesMapping = [RKObjectMapping mappingForClass:[CurrentGames class]];
[CurrentGamesMapping addAttributeMappingsFromDictionary:@{@"GameId":@"GameId",
@"StartTime":@"StartTime"}];
[CurrentGamesMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Items" toKeyPath:@"Items" withMapping:itemsMapping]];
// Response descriptor for all classes
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:CurrentGamesMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
// Requesting data with URL
NSURL *URL = [NSURL URLWithString:@"xxx.net"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation =[[RKObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
NSLog(@"answers mapping %@ ",answersMapping);
NSLog(@"items mapping %@ ",itemsMapping);
NSLog(@"currentGames mapping %@ ",CurrentGamesMapping);
}
答案 0 :(得分:1)
此属性:
@property (nonatomic) answers *Answers;
应该是:
@property (strong, nonatomic) NSArray *Answers;
因为你有一个答案对象列表。
此属性:
@property (nonatomic) items *Items;
有同样的问题。这两个错误将阻止您的映射按预期工作。