我有這個JSON響應,我無法弄清楚如何映射。它看起來像這樣:RestKit對象映射問題
{
"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。
你設置一個響應描述的地方? –
我只是不知道如何設置'RKObjectMapping'和'RKResponseDescriptor'正確的方式..這是我的問題。 –