iOS创建复杂的JSON

时间:2013-09-30 16:48:54

标签: ios objective-c json restkit-0.20

我一直在努力解决这个问题已有一段时间了,我无法弄清楚为什么JSON序列化不起作用。什么似乎是一个简单的任务,它花了太长时间。

我的模型类列在下面。用户对象具有NSArray的Education和Employment对象。

我正在使用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始终为“NO”。当我强制创建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]];

0 个答案:

没有答案