0

是否可以使用具有以下 JSON 结构的键映射,或者我们是否必须手动执行值循环?例子会很棒。他们的 rest api 并没有真正将现实世界的对象映射到标准的对象结构中,而只是在 XML 属性之后建模。:(

THINGS{
"lastModifiedDate": "2012-02-23-08.43.16.916000",
"myList": [{
  "attributeList": [
                    {"id": "","name": "Content Level","val": "Introductory"},
                    {"id": "","name": "Session Type","val": "Business Overview"},
                    {"id": "20110616053537016","name": "Speaker","val": "Jim Kim, Company1"},
                    {"id": "20110616053526559","name": "Speaker","val": "Bob Ironman, Company2"},
                    {"id": "20110803145027914","name": "Speaker","val": "Kristine Thomas, Company3"},
                    {"id": "","name": "Room","val": "Banyan"},
                    {"id": "","name": "Industry","val": "Cross Industry"},
                    {"id": "","name": "Loc","val": "Stadium I"},
                    {"id": "","name": "Topic Tag","val": "CMS Systems"},
                    {"id": "","name": "Status","val": "Accepted"},
                    {"id": "","name": "Sub-Event","val": "Leadership"},
                    {"id": "","name": "Session","val": "LVI"},
                    {"id": "","name": "SubTrack","val": "None"},
                    {"id": "","name": "Track","val": "Business Value Outsourcing"}
  ],

"active": true,
"desc": "This is a really cool thing",
"end": "16:00",
"id": "2011080146112",
"num": "1002A",
"start": "15:00",
"title": "The thing title"
  }
 ]
}
4

2 回答 2

2

是的,使用 RestKit 是可能的。

您可以从官方wiki获取阅读本文所需的所有文档,在这里您可以了解如何做您需要做的事情。希望这对你有帮助!

首先,每个实体需要三个对象,一个用于 ThingsList,另一个用于表示一个事物,另一个用于表示一个 ThingAttribute。让我们从最基本的元素 ThingAttribute 开始:

SOAttribute.h

#import <Foundation/Foundation.h>

@interface SOAttribute : NSObject{
    NSNumber *attributeId; //I use this nomenclature because the word 'id' is reserved, same for other objects.
    NSString *name;
    NSString *val;
}

@end

SO属性.m

@implementation SOAttribute
@end

这将让您表示 JSON 的这一部分:

{"id": "","name": "Content Level","val": "Introductory"}

现在我们将 Thing 定义如下:

SOThing.h

#import <Foundation/Foundation.h>

@interface SOThing : NSObject{

    NSArray *attributeList; //This represent the list of attributes defined before
    BOOL active;
    NSString *desc;
    NSString *title;
    NSString *end;
    NSString *start;
    NSString *num;
    NSNumber *thingId;

}

@end

SOThing.m

#import "SOThing.h"
@implementation SOThing
@end

如您所见,您必须将attributeList 定义为NSArray。RestKit 会自动知道您需要在那里接收属性列表。所以我们定义了以下结构:

{
    "attributeList": [...],
    "active": true,
    "desc": "This is a really cool thing",
    "end": "16:00",
    "id": "2011080146112",
    "num": "1002A",
    "start": "15:00",
    "title": "The thing title"
}

最后,您按如下方式定义事物列表:

SOThingList.h

#import <Foundation/Foundation.h>

@interface SOThingList : NSObject{
    NSDate *lastModifiedDate;
    NSArray *myList;
}
@end

SoThingList.m

#import "SOThingList.h"
@implementation SOThingList
@end

和以前一样,我们需要一个事物列表,所以我们将 myList 定义为一个 NSArray。

现在,这很容易,这里有你向 RestKit 指示如何映射每个实体的魔法。在您的 App Delegate 上放置以下代码:

#import "SOThingList.h"
#import "SOThing.h"
#import "SOAttribute.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{


    RKObjectMappingProvider *mappingProvider = [RKObjectManager sharedManager].mappingProvider;

    RKObjectMapping *mappingForAttribute = [RKObjectMapping mappingForClass:[SOAttribute class]];
    //This can be mapped directly
    [mappingForAttribute mapAttributes:@"name",@"val", nil];
    //here you indicate the special case, id->attributeId on our object
    [mappingForAttribute mapKeyPath:@"id" toAttribute:@"attributeId"];
    //Set the new mapping into the mapping provider.
    [mappingProvider addObjectMapping:mappingForAttribute];

    RKObjectMapping *mappingForThing = [RKObjectMapping mappingForClass:[SOThing class]];
    //Same as before, these attributes can be mapped directly
    [mappingForThing mapAttributes:@"active",@"title",@"end",@"start",@"desc",@"num", nil];
    [mappingForThing mapKeyPath:@"id" toAttribute:@"thingId"];
    //Here I indicate that any object on NSArray on attributeList should be mapped using SOAttribute mapping defined above.
    [mappingForThing mapKeyPath:@"attributeList" toRelationship:@"attributeList" withMapping:mappingForAttribute];
    [mappingProvider addObjectMapping:mappingForThing];

    RKObjectMapping *mappingForThingsList = [RKObjectMapping mappingForClass:[SOThingList class]];
    [mappingForThingsList mapAttributes:@"lastModifiedDate", nil];
    [mappingForThingsList mapKeyPath:@"myList" toRelationship:@"myList" withMapping:mappingForThing];
    [mappingProvider addObjectMapping:mappingForThingsList];

}

综上所述,您可以通过以下方法使用块获取您的事物列表:

一些对象.m

- (void) get: (NSString *) resourcePath onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{

    RKObjectMapping *mapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[SOThingList class]];
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader* loader) {
        loader.objectMapping = mapping;
        loader.delegate = self;
        loader.onDidLoadObject = loadBlock;

        loader.onDidFailWithError = ^(NSError * error){
            NSLog(@"%@",error);
        };
        loader.onDidFailLoadWithError = failBlock;
        loader.onDidLoadResponse = ^(RKResponse *response) {
            //Do something
        };

    }];

}

而你这样使用它......

[someObjectInstance get:@"http://mydomain.com/mywebservice/" onLoad:^(id object){
    SOThingList *thingList = (SOThingList *) object;
    //Do something with your shiny thingList
} onError:^(NSError *error) {
    //Display an error message :)
}];
于 2012-06-04T00:45:22.833 回答
0

您始终可以使用 Mac App Store 中的 Objectify(15 美元)或 JSON Accelerator(0.99 美元)等工具来帮助生成此 JSON 的模型。然后,当您拥有数组时,您可以使用类似 NSArray 的东西

- (void)makeObjectsPerformSelector:(SEL)aSelector

让所有对象寻找一个对象。

我个人更喜欢使用块和

- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block循环遍历事物

它与 RestKit 没有直接关系,但这可能是一种帮助你的方法。

于 2012-05-14T19:46:13.497 回答