0

我正在尝试映射此 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"
}

但我遇到了一些问题。在一遍又一遍地浏览了 Restkit 的对象映射概述之后,我将我的数据划分为 3 个不同的类。CurrentGames,项目和答案。我将答案类映射为无键路径,如中所述所述,对于项目类,我创建了与答案类的关系。我创建了另一个从项目到 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>"
)

我的课程:

答案.h

#import <Foundation/Foundation.h>

@interface answers : NSObject

@property (nonatomic,copy) NSString * answerss;
@end

项目.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

当前游戏.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);
}
4

1 回答 1

1

这个性质:

@property (nonatomic) answers *Answers;

应该:

@property (strong, nonatomic) NSArray *Answers;

因为你有一个答案对象列表。

这个性质:

@property (nonatomic) items *Items;

有同样的问题。这 2 个错误将阻止您的映射按预期工作。

于 2013-10-09T09:59:33.480 回答