Domanda

Ho questa risposta JSON che non riesco a capire come mappare.Sembra questo:

{
  "responseError": null,
  "displayMenuList": {
    "MenuList": [
      {
        "ID": "3223",
        "Name": "Main",
        "AddressURL": "www.mysite.com",
        "DisplayType": "True",
        "ImageURL": "main.png",
        "NotSplitBUser": "True",
        "ParentCategoryId": "3223",
        "PrivateUser": "True",
        "SortOrder": 1,
        "SplitBUser": "True",
        "TargetURL": "_self"
      },
      {
        "ID": "3307",
        "Name": "Contact",
        "AddressURL": "www.mysite.com",
        "DisplayType": "True",
        "ImageURL": "service.png",
        "NotSplitBUser": "True",
        "ParentCategoryId": "3224",
        "PrivateUser": "True",
        "SortOrder": 0,
        "SplitBUser": "True",
        "TargetURL": "_self"
      },
      {
        "ID": "3298",
        "Name": "Call Us",
        "AddressURL": "www.mysite.com",
        "DisplayType": "True",
        "ImageURL": "service.png",
        "NotSplitBUser": "True",
        "ParentCategoryId": "3224",
        "PrivateUser": "True",
        "SortOrder": 1,
        "SplitBUser": "True",
        "TargetURL": "_self"
      },
      {
        "ID": "3224",
        "Name": "Service",
        "AddressURL": "www.mysite.com",
        "DisplayType": "True",
        "ImageURL": "service.png",
        "NotSplitBUser": "True",
        "ParentCategoryId": "3224",
        "PrivateUser": "True",
        "SortOrder": 2,
        "SplitBUser": "True",
        "TargetURL": "_self"
      }
    ]
  },
  "responseCurrentBillState": null,
  "responseGetPcrfSubBuckets": null,
  "userData": {
    "abroadInd": null,
    "accountType": "B",
    "customerId": "",
    "fullName": "Juan",
    "subscriberNumber": ""
  }
}
.

Non riesco a capire come mappare quegli oggetti, ho creato oggetto chiamato RKSideMenu, anche un oggetto chiamato RKUserData Sembrano questo:

@interface RKSideMenu : NSObject
@property (copy, nonatomic) NSString *addressURL;
@property (copy, nonatomic) NSString *displayType;
@property (copy, nonatomic) NSNumber *id_number;
@property (copy, nonatomic) NSString *imageURL;
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) BOOL splitBUser;
+ (NSDictionary*)getAttributes;
@end

@implementation RKSideMenu
+ (NSDictionary*)getAttributes
{
    return [NSDictionary dictionaryWithObjects:@[@"addressURL", @"displayType", @"id_number", @"imageURL", @"name", @"splitBUser"]
                                       forKeys:@[@"AddressURL", @"DisplayType", @"ID", @"ImageURL", @"Name", @"SplitBUser"]];
}
@end

@interface RKUserData : NSObject
@property (copy, nonatomic) NSString *abroadInd;
@property (copy, nonatomic) NSString *accountType;
@property (copy, nonatomic) NSString *customerID;
@property (copy, nonatomic) NSString *fullName;
@property (copy, nonatomic) NSString *subscriberNumber;
+ (NSDictionary*)getAttributes;
@end

@implementation RKUserData
+ (NSDictionary*)getAttributes
{
    return [NSDictionary dictionaryWithObjects:@[@"abroadInd", @"accountType", @"customerID", @"fullName;", @"subscriberNumber"]
                                       forKeys:@[@"abroadInd", @"accountType", @"customerId", @"fullName;", @"subscriberNumber"]];
}
@end
.

Ho iniziato a mappare con quei due metodi, ma che ho bloccato e non so cosa fare.Guardo https://github.com/restkit/restkit/wiki/object-mapping , ma ancora non riuscivo a farlo bene.

RKObjectMapping *sideMenuMapping = [RKObjectMapping mappingForClass:[RKSideMenu class]];
[sideMenuMapping addAttributeMappingsFromDictionary:[RKSideMenu getAttributes]];

RKObjectMapping *userDataMapping = [RKObjectMapping mappingForClass:[RKUserData class]];
[userDataMapping addAttributeMappingsFromDictionary:[RKUserData getAttributes]];
.

Grazie in anticipo!

Modifica: JSON in cima sostituito con Real JSON dal server.

È stato utile?

Soluzione

Quindi cosa devi fare per configurare la mappatura è prima di tutto definire l'URL di base dell'API, come:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"URL_TO_YOUR_API"]];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
.

Quindi è necessario impostare un descrittore di risposta per l'URL che emette JSON sopra:

[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:sideMenuMapping
                                                                                                    method:RKRequestMethodAny
                                                                                               pathPattern:@"/mainScreenData"
                                                                                                   keyPath:@"displayMenuList.MenuList"
                                                                                               statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
.

"rkrequestmethodany" dovrebbe essere sostituito dal metodo di richiesta che stai utilizzando ad es."Rkrequestmethodget".

Quindi recupera gli oggetti chiamando:

  [[RKObjectManager sharedManager] getObjectsAtPath:@"/mainScreenData"
                   parameters:nil
                      success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

                          NSArray *sideMenuList = [NSMutableArray arrayWithArray:[mappingResult array]];
                      }
                      failure:^(RKObjectRequestOperation *operation, NSError *error) {
                          NSLog(@"%@",[error localizedDescription]);
                      }];  
.

Consiglierei vivamente di dare un'occhiata alle demo fornite da Restkit.Questo rende l'intero processo molto più chiaro.

Cheers,
Sebastian

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top