RestKit嵌套对象不完全序列化

时间:2014-05-28 21:12:29

标签: json serialization nested restkit

以下是JSON对象:

{
    "Id": 1107,
    "Name": "QA1-All Different Questions",
    "Description": "This survey should contain all...",
    "OwnerGroupId": 101,
    "SandboxMode": false,
    "Created": "0001-01-01T00:00:00",
    "Modified": "0001-01-01T00:00:00",
    "ModifiedByUserId": 0,
    "Active": false,
    "QuestionGroups": [{
        "Id": 101,
        "Name": "Different Types of Questions with longer name for testing",
        "SurveyId": 1107,
        "Description": "",
        "SandboxMode": false,
        "TypeValueId": 151,
        "QuickList": false,
        "EnumElements": "",
        "OrderBy": 101,
        "Created": "0001-01-01T00:00:00",
        "Modified": "0001-01-01T00:00:00",
        "ModifiedByUserId": 0,
        "Active": false,
        "ParentId": 0,
        "Repeatable": false,
        "Questions": [{
            "Id": 103,
            "TypeValueId": 101,
            "TypeValueName": "Single Choice",
            "QuestionGroupId": 101,
            "QuestionGroupName": "Different Types ...",
            "OrderBy": 103,
            "SandboxMode": false,
            "Prompt": "Single Choice Question",
            "HelpText": "Why does she not have a the latest phone?",
            "DefaultAnswer": "Yes",
            "AnswerChoice": "Yes, No, Maybe, Not Sure",
            "MaxLength": 50,
            "Width": 25,
            "Required": true,
            "Hidden": false,
            "Nullable": false,
            "ReadOnly": false,
            "Created": "2013-06-28T11:59:57.393",
            "Modified": "2013-09-12T13:25:15.9",
            "ModifiedByUserId": 20097,
            "ModifiedByUserName": "Bill",
            "Rules": []
        }, {
            "Id": 104,
            "TypeValueId": 102,
            "TypeValueName": "Multiple Choice",
            "QuestionGroupId": 101,
            "QuestionGroupName": "Different Types of Questions",
            "OrderBy": 104,
            "SandboxMode": false,
            "Prompt": "Multiple Choice Question",
            "HelpText": "",
            "DefaultAnswer": "Can",
            "AnswerChoice": "Can, I, Select, More, Than, One, Answer",
            "MaxLength": 50,
            "Width": 25,
            "Required": true,
            "Hidden": false,
            "Nullable": false,
            "ReadOnly": true,
            "Created": "2013-06-28T12:01:31.167",
            "Modified": "2013-06-28T12:01:34.89",
            "ModifiedByUserId": 20094,
            "ModifiedByUserName": "A C",
            "Rules": []
        }],
        "Rules": [],
        "Children": []
    }]
}

以下是我的课程标题:

@interface Survey : NSObject

    @property (assign) int Id;
    @property (nonatomic, copy) NSString* Name;
    @property (nonatomic, copy) NSString* Description;
    @property NSInteger OwnerGroupId;
    @property (assign) bool SandboxMode;
    @property (nonatomic, retain) NSDate* Created;
    @property (nonatomic, retain) NSDate* Modified;
    @property NSInteger ModifiedByUserId;
    @property (assign) bool Active;
    @property (nonatomic, copy) NSString* ActivityTypeName;
    @property (nonatomic, copy) NSArray* QuestionGroups;

@end

@interface QuestionGroup : NSObject

@property (assign) int Id;
@property (assign) int SurveyId;
@property (nonatomic, copy) NSString* Name;
@property (nonatomic, copy) NSString* Description;
@property (assign) bool QuickList;
@property (assign) int TypeValueId;
@property Boolean Active;
@property Boolean SandboxMode;
@property (nonatomic, strong) NSDate* Created;
@property (nonatomic, strong) NSDate* Modified;
@property (nonatomic, copy) NSArray* Questions; // Questions in this Question Group
@property (nonatomic, copy) NSArray* Rules; // Rules for this entire Question Group
@property (nonatomic, copy) NSArray* Children; // Children Question Groups

@end

@interface Question : NSObject

@property (assign) int Id;
@property (assign) int QuestionGroupId;
@property (assign) int TypeValueId;
@property (nonatomic, copy) NSString* TypeValueName;
@property (nonatomic, copy) NSString *Prompt;
@property (assign) bool ReadOnly;
@property (assign) bool Hidden;
@property (assign) bool SandboxMode;
@property (assign) bool Required;
@property (assign) bool Nullable;
@property int OrderBy;
@property int MaxLength;
@property int Width;
@property (nonatomic, strong) NSDate *Created;
@property (nonatomic, strong) NSDate *Modified;
@property (nonatomic, copy) NSString *DefaultAnswer;
@property (nonatomic, copy) NSString *AnswerChoice;
@property (nonatomic, copy) NSString *HelpText;
@property (nonatomic, copy) NSArray *Rules; // Rules for this Question

@end

下面是我的RestKit 0.20实现,问题不是所有的序列化只有Survey及其QuestionGroups,问题永远不会绑定,在我得到结果后Class Survey有所有属性,它有QuestionGroups,但那些QuestionGroups(JSON中有1个),没有2个问题,NSArray是nil且JSON有正确的数据......任何想法为什么它不能从你能看到的东西开始工作?

// setup object mappings
RKObjectMapping *objMapping = [RKObjectMapping mappingForClass:[Survey class]];
[objMapping addAttributeMappingsFromArray:@[@"Id", @"Name", @"Description", @"OwnerGroupId", @"SandboxMode", @"Created", @"Modified", @"ModifiedByUserId", @"Active"]];

RKObjectMapping *objMappingQuestionGroup = [RKObjectMapping mappingForClass:[QuestionGroup class]];
[objMappingQuestionGroup addAttributeMappingsFromArray:@[@"Id", @"SurveyId", @"Name", @"Description", @"SandboxMode", @"QuickList", @"TypeValueId", @"Active", @"Created", @"Modified"]];

//[objMapping addRelationshipMappingWithSourceKeyPath:@"QuestionGroups" mapping:objMappingQuestionGroup];

RKObjectMapping *objMappingQuestion = [RKObjectMapping mappingForClass:[Question class]];
[objMappingQuestion addAttributeMappingsFromArray:@[@"Id", @"QuestionGroupId", @"TypeValueId", @"TypeValueName", @"Prompt", @"ReadOnly", @"Hidden", @"SandboxMode", @"Required", @"Nullable", @"OrderBy", @"MaxLength", @"Width", @"Created", @"Modified", @"DefaultAnswer", @"AnswerChoice", @"HelpText"]];

[objMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"QuestionGroups"
                                                                                 toKeyPath:@"QuestionGroups"
                                                                               withMapping:objMappingQuestionGroup]];

[objMappingQuestionGroup addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Questions"
                                                                                     toKeyPath:@"Questions"
                                                                                   withMapping:objMappingQuestion]];

0 个答案:

没有答案