我有这个JSON响应,我无法弄清楚如何映射。它看起来像这样:
{
"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": ""
}
}
我无法弄清楚如何映射这些对象,我创建了一个名为RKSideMenu
的对象,还有一个名为RKUserData
的对象,它们看起来像这样:
@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
我开始使用这两种方法进行映射,但是我坚持并且不知道该怎么做。我看https://github.com/RestKit/RestKit/wiki/Object-mapping,但仍然无法做到。
RKObjectMapping *sideMenuMapping = [RKObjectMapping mappingForClass:[RKSideMenu class]];
[sideMenuMapping addAttributeMappingsFromDictionary:[RKSideMenu getAttributes]];
RKObjectMapping *userDataMapping = [RKObjectMapping mappingForClass:[RKUserData class]];
[userDataMapping addAttributeMappingsFromDictionary:[RKUserData getAttributes]];
提前致谢!
编辑:顶部的Json替换为来自服务器的真实json。
答案 0 :(得分:1)
因此,为了设置映射,您需要做的是首先定义API的基本URL,如下所示:
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"URL_TO_YOUR_API"]];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
然后你需要为输出上面的json的url设置一个响应描述符:
[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:sideMenuMapping
method:RKRequestMethodAny
pathPattern:@"/mainScreenData"
keyPath:@"displayMenuList.MenuList"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
“RKRequestMethodAny”应替换为您正在使用的请求方法,例如“RKRequestMethodGET”。
然后你只需要调用:
来检索对象 [[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]);
}];
我强烈建议您查看RestKit提供的演示。这使得整个过程更加清晰。
干杯,
塞巴斯蒂安