0

我已经在这个问题上苦苦挣扎了一段时间,我无法弄清楚为什么 JSON 序列化不起作用。这似乎是一项简单的任务,但花费的时间太长了。

我在下面列出了我的模型类。用户对象具有教育和就业对象的 NSArray。

我正在使用 RestKit 并且所有关系都已经建立,我可以看到参数字典正在生成数据是正确的。为了进一步调试问题,我使用以下代码制作 JSON。

NSDictionary *parameters = [RKObjectParameterization parametersWithObject:self.user requestDescriptor:delegate.postUserRequestDescriptor error:&error];

    // Serialize the object to JSON
    BOOL isTurnableToJSON = [NSJSONSerialization
                             isValidJSONObject: parameters];
    if(isTurnableToJSON){
        NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
    }

isTurnableToJSON 始终为“否”。当我强制创建 JSON 时,出现以下错误。

** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Education)'

如果我删除教育与用户的关系,就业也会引发同样的错误。

这是我的用户、教育和位置模型。

@interface User : NSObject

@property (strong, nonatomic) NSString *first_name;
@property (strong, nonatomic) NSString *middle_name;
@property (strong, nonatomic) NSString *last_name;
@property (strong, nonatomic) NSString *profile_picture;
@property (strong, nonatomic) NSString *display_name;
@property (nonatomic, strong) NSArray *education;
@property (nonatomic, strong) NSArray *employment;
@property (nonatomic, strong) NSMutableArray *skills;
@property (strong, nonatomic) NSString *about_you;
@end

@interface Education : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *graduation_date;
@property (nonatomic, strong) NSString *major;
@property (nonatomic, strong) NSString *degree;
@property (nonatomic, strong) Location *location;
@property (nonatomic) NSNumber *current;
@end

@interface Location : NSObject

@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *state;
@property (nonatomic, strong) NSString *country;
@end

在这一点上,我失去了任何想法,任何帮助将不胜感激。

更新: RestKit 关系映射。

RKObjectMapping *locationMap = [RKObjectMapping mappingForClass:(Location.class)];
[locationMap addAttributeMappingsFromDictionary:@{@"city":@"city",@"state":@"state",@"country":@"country"}];

RKObjectMapping *educationMap = [RKObjectMapping mappingForClass:(Education.class)];
[educationMap addAttributeMappingsFromDictionary:@ { @"name":@"name",@"graduation_date":@"graduation_date",@"major":@"major", @"degree":@"degree",@"current":@"current"}];
[educationMap addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"location" toKeyPath:@"location" withMapping:locationMap]];

RKObjectMapping *employmentMap =[RKObjectMapping mappingForClass:(Employment.class)];
[employmentMap addAttributeMappingsFromDictionary:@{@"company":@"company",@"occupation":@"occupation", @"start_date":@"start_date",@"end_date":@"end_date",@"current":@"current"}];


RKObjectMapping *userPutRequestMapping = [RKObjectMapping requestMapping];
[userPutRequestMapping addAttributeMappingsFromDictionary:putUserMap];

[userPutRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"education" toKeyPath:@"education" withMapping:[educationMap inverseMapping]]];
[userPutRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"employment" toKeyPath:@"employment" withMapping:[employmentMap inverseMapping]]];



USER PUT REQUEST

    [objectManager addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:userPutRequestMapping
                                                                              objectClass:[User class]
                                                                              rootKeyPath:nil
                                                                                   method:RKRequestMethodPUT]];
4

0 回答 0