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 つの異なるクラスに設定しました。現在のゲーム、アイテム、および回答。hereで説明されているように、回答クラスをキーパスなしとしてマップし、アイテムクラスについては、回答クラスへの関係を作成しました。そして、items から currentgames クラスへの別の関係を作成しました。私はマッピング部分を正しく行ったと信じています。しかし、データを適切に取得していないため、コードに何かが欠けています。私はこれを1週間設定しようとしています。誰かが助けてくれれば、大いに感謝します!

これは私のコンソールがどのように見えるかです:

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

私が使用しているView Controller:

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 に答える