gpt4 book ai didi

ios - 无法使用 restkit 映射嵌套数组 json

转载 作者:行者123 更新时间:2023-11-29 02:11:19 24 4
gpt4 key购买 nike

我如何映射这个 json

{
"json": [{
"json_department": [{
"department": {
"id": 1,
"inst_id": 1,
"dept_name": "Department",
"description": "test"
},
"needDelete": true
}],
"json_subjects": [{
"subjects": [{
"id": 1,
"department_id": 1,
"subject_name": "Sub 1"
}, {
"id": 2,
"department_id": 1,
"subject_name": "Sub 2"
}],
"needDelete": true
}]
}]
}
@interface class_department : NSObject

@property(nonatomic, assign) NSInteger dept_id;
@property(nonatomic, assign) NSInteger inst_id;
@property(nonatomic, strong) NSString *dept_name;
@property(nonatomic, strong) NSString *description_;

@end

_

@interface class_department_list : NSObject

@property(nonatomic, strong) class_department *department;
@property(nonatomic, assign) BOOL needDelete;

@end

-

@interface sync_json : NSObject

@property(nonatomic, strong) NSMutableArray *json_department;
@property(nonatomic, strong) NSMutableArray *json_subjects;

@end

-

-(RKResponseDescriptor *)getResponseDescriptor
{

RKObjectMapping *class_department_mapping = [RKObjectMapping mappingForClass:[class_department class]];
[class_department_mapping addAttributeMappingsFromDictionary:@{
@"id":@"dept_id",
@"inst_id":@"inst_id",
@"dept_name":@"dept_name"
@"description":@"description_",
}];


RKObjectMapping *class_department_list_mapping = [RKObjectMapping mappingForClass:[class_department_list class]];
[class_department_list_mapping addAttributeMappingsFromDictionary:@{
@"needDelete":@"needDelete"
}];

[class_department_list_mapping addPropertyMapping:[RKRelationshipMapping
relationshipMappingFromKeyPath:@"json_department.department"
toKeyPath:@"department"
withMapping:class_department_mapping]];


RKObjectMapping *json_mapping = [RKObjectMapping mappingForClass:[sync_json class]];

[json_mapping addPropertyMapping:[RKRelationshipMapping
relationshipMappingFromKeyPath:@"json_department"
toKeyPath:@"json_department"
withMapping:class_department_list_mapping]];



NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:json_mapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"json"
statusCodes:statusCodes];

return responseDescriptor;

}

好的,这是我用来映射 json 的代码。我得到了“needDelete”的结果,但没有映射到 class_department。请给我一个方法来做到这一点。

谢谢。希努拉格 KR

最佳答案

抱歉,请尽量忘记这个丑陋的实现。 Mantle 为您做一切事情都很简单。项目:https://github.com/Mantle/Mantle示例:http://www.objc.at/mantle

关于ios - 无法使用 restkit 映射嵌套数组 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29257706/

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