RestKit not mapping recursive JSON to objects

百般思念 提交于 2019-12-24 14:57:23

问题


Edit 2 For reasons that I dont quite understand, adding the response descriptor directly to httpsRKManager, instead of the app layering, got RK to recognize the "Response" response descriptor. The issue now is that it seems not to recognize the attribute mapping for "ErrorStatus" /end edit

I have three issues. First Shops and the recursive object Shop do not show up as part of the LLSResult object. Second the objects are not populated from the result, and third, is there a way to skip Shops altogether. The context is I am migrating an existing app from a Ruby server to a .NET server with a different api. To compound matters. two weeks ago I had never touched a Mac. let alone any of the ecosystem.

Edit 2: The Response Descriptor fix


    NSString *path = [[ConfigManager sharedInstance] getEventsURL];

    [[[AppCore sharedInstance] httpsRKManager]addResponseDescriptor:
            [RKResponseDescriptor responseDescriptorWithMapping:[LLSResponse jsonMapping]
                                method:RKRequestMethodAny
                                pathPattern:path
                                keyPath:@"Response"
                                statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

    // This didn work
    //[BaseRO addResponseDescriptorForPathPattern: [[ConfigManager sharedInstance] getEventsURL]
    //                                withMapping:[LLSResponse jsonMapping]];

The log for edit 2:


        } to object  with object mapping (null)
    2015-12-11 10:19:24.805 The Clymb[5234:149794] D restkit.object_mapping:RKPropertyInspector.m:131 Cached property inspection for Class 'LLSResponse': {
        debugDescription =     {
            isPrimitive = 0;
            keyValueCodingClass = NSString;
            name = debugDescription;
        };
        description =     {
            isPrimitive = 0;
            keyValueCodingClass = NSString;
            name = description;
        };
        events =     {
            isPrimitive = 0;
            keyValueCodingClass = Events;
            name = events;
        };
        hash =     {
            isPrimitive = 1;
            keyValueCodingClass = NSNumber;
            name = hash;
        };
        shops =     {
            isPrimitive = 0;
            keyValueCodingClass = Shops;
            name = shops;
        };
        status =     {
            isPrimitive = 0;
            keyValueCodingClass = ErrorStatus;
            name = status;
        };
    }
    2015-12-11 10:19:24.805 The Clymb[5234:149797] D restkit.object_mapping:RKMappingOperation.m:592 Mapping one to one relationship value at keyPath 'ErrorStatus' to 'ErrorStatus'
    2015-12-11 10:19:24.806 The Clymb[5234:149797] T restkit.object_mapping:RKMappingOperation.m:550 Performing nested object mapping using mapping  ErrorStatus> for data: {
        "@ID" = "";
        "@Status" = OK;
    }
    2015-12-11 10:19:24.806 The Clymb[5234:149797] D restkit.object_mapping:RKMappingOperation.m:868 Starting mapping operation...
    2015-12-11 10:19:24.807 The Clymb[5234:149797] T restkit.object_mapping:RKMappingOperation.m:869 Performing mapping operation:  for 'ErrorStatus' object. Mapping values from object {
        "@ID" = "";
        "@Status" = OK;
    } to object  with object mapping (null)

end of edit 2

The JSON



        {
    Response: {
        ErrorStatus: {
            @ID: "",
            @Status: "OK"
        },
        Events: {
            Event: [
                {
                    @Title: "Test - Hero 1",
                    @ID: "00010033005800000000",
                    @Start: "2015-12-07 09:00:00Z",
                    @End: "2015-12-31 08:00:00Z",
                    @Status: "Active",
                    @Image_Small: "http://www.leftlanesports.com/App_Themes/Default/graphics/Events/291_00010033005800000000.jpg",
                    @Image_Large: "http://www.leftlanesports.com/App_Themes/Default/graphics/Events/447_00010033005800000000.jpg",
                    @TypeCode: "EVTH1",
                    Description: {
                    #cdata-section: "yo"
                    },
                    ShortDescription: {
                    #cdata-section: "50%##Hero Event 1"
                    }
                },
            ...
            ]
        },
        Shops: {
            Shop: [
                {
                    @Title: "Adventures",
                    @ID: "00030000000000000000"
                },
                {
                    @Title: "Apparel",
                    @ID: "00080000000000000000",
                    Shop: [
                        {
                            @Title: "Mens",
                            @ID: "00080001000000000000",
                            Shop: [
                                {
                            @Title: "Accessories",
                            @ID: "00080001003700000000",
                            Shop: [

The .h file



    #import 
    #import "BaseRO.h"
    #import "EventDO.h"

    @class LLSResponse;
    @class Events;
    @class ErrorStatus;

    @interface LLSResponse : NSObject 
    @property (nonatomic, strong) ErrorStatus * status;
    @property (nonatomic, strong) Events * events;
    - getAllEvents;
    @end

    @interface ErrorStatus : NSObject 
    @property (nonatomic, copy) NSString * _id;
    @property (nonatomic, copy) NSString * Status;
    @end


    @interface Events : NSObject ;
    @property (nonatomic, strong) NSMutableArray *events;

    @end

    @interface Shop : NSObject ;
    @property (nonatomic, copy) NSString * _id;
    @property (nonatomic, copy) NSString * title;
    @property (nonatomic, strong) NSMutableArray *shops;

    @end

    @interface Shops : NSObject ;
    @property (nonatomic, strong) NSMutableArray * shops;

    @end

The relevant parts after edit 2 seem to be Response.jsonMapping and ErrorStatus.jsonMapping.

The .m file



    @implementation LLSResponse

    + (RKObjectMapping *)jsonMapping
    {
        RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);    //??? debugging


        RKObjectMapping * entityMapping = [RKObjectMapping mappingForClass:[LLSResponse class]];

        [entityMapping addPropertyMapping:
         [RKRelationshipMapping relationshipMappingFromKeyPath:@"ErrorStatus"
                                                     toKeyPath:@"ErrorStatus"
                                                   withMapping:[ErrorStatus jsonMapping]]];
        [entityMapping addPropertyMapping:
         [RKRelationshipMapping relationshipMappingFromKeyPath:@"Shops"
                                                     toKeyPath:@"Shops"
                                                   withMapping:[Shops jsonMapping]]];
        [entityMapping addPropertyMapping:
         [RKRelationshipMapping relationshipMappingFromKeyPath:@"Events"
                                                     toKeyPath:@"Events"
                                                   withMapping:[Events jsonMapping]]];

        return entityMapping;
    }

    - (NSArray *) getAllEvents
    {
        Events * events = self.events;
        NSArray *allEvents = events.events;
        return allEvents;
    }
    @end


    @implementation ErrorStatus

    + (RKObjectMapping *)jsonMapping
    {
        RKObjectMapping * entityMapping = [RKObjectMapping mappingForClass:[ErrorStatus class]];
        [entityMapping addAttributeMappingsFromDictionary:@{
                                                            @"@ID" : @"_id",
                                                            @"@Status" : @"Status"
                                                            }];
        return entityMapping;
    }

    @end


    @implementation Events
    + (RKObjectMapping *)jsonMapping
    {
        RKObjectMapping * entityMapping = [RKObjectMapping mappingForClass:[Events class]];
        [entityMapping addPropertyMapping:
         [RKRelationshipMapping relationshipMappingFromKeyPath:@"Event"
                                                     toKeyPath:@"Event"
                                                   withMapping:[EventDO jsonMapping]]];    
        return entityMapping;
    }

    @end

    @implementation Shop
    + (RKObjectMapping *)jsonMapping
    {
        RKObjectMapping * entityMapping = [RKObjectMapping mappingForClass:[Shop class]];

        [entityMapping addAttributeMappingsFromDictionary:@{

                                                            @"@ID" : @"_id",
                                                            @"@Title" : @"title"

                                                            }];

        //RKEntityMapping * shopMapping = [RKEntityMapping mappingForClass: [Shop class]];
        //[entityMapping addRelationshipMappingWithSourceKeyPath:@"Shop" mapping:entityMapping];
        [entityMapping addPropertyMapping:
            [RKRelationshipMapping relationshipMappingFromKeyPath:@"Shop"
                                                     toKeyPath:@"Shop"
                                                   withMapping:entityMapping]];
        return entityMapping;
    }

    @end

    @implementation Shops
    + (RKObjectMapping *)jsonMapping
    {
        RKObjectMapping * entityMapping = [RKObjectMapping mappingForClass:[Shops class]];
        [entityMapping addPropertyMapping:
         [RKRelationshipMapping relationshipMappingFromKeyPath:@"Shop"
                                                     toKeyPath:@"Shop"
                                                   withMapping:[Shop jsonMapping]]];
        return entityMapping;
    }

    @end

When I run the app in the Xcode simulator I find that it has successfully called the server and the above JSON has been returned. The LLSResponse object, the ErrorStatus Object, and the Events object have been created. However the Shops object has not. So there is a problem in the Shops/Shop mapping, but I cant see it. When I examine the objects none of them have been populated. I dont know whether this is a separate issue or a consequence of the Shops problem.

Shops is actually extraneous data returned by the API. Is there a way to skip it? What happens if it is not mapped at all; is it an error?

EDIT 2: Text deleted.

Thanks

来源:https://stackoverflow.com/questions/34167180/restkit-not-mapping-recursive-json-to-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!