gpt4 book ai didi

ios - RestKit对象映射问题

转载 作者:行者123 更新时间:2023-11-29 03:21:38 25 4
gpt4 key购买 nike

我有这个 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。

最佳答案

因此,为了设置映射,您需要做的是首先定义 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 提供的演示。这使整个过程更加清晰。

干杯,
塞巴斯蒂安

关于ios - RestKit对象映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21019913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com