对象映射不适用于RESTKit对象映射

时间:2013-10-05 05:11:27

标签: ios objective-c restkit restkit-0.20

我正在使用RESTKit进行GET请求,当我进行对象映射时,它不起作用。但是,它始终表示它在映射结果中映射了一个对象,但我不知道它是什么或如何让它映射其余的对象。我究竟做错了什么导致映射结果显示为一个映射的对象,如何让它映射所有正确的对象?

以下是映射结果:

Mapping Result: (
    "<Info: 0x9c91130>"
)

这是我的GET请求: // RK记录     RKLogConfigureByName(“RestKit / Network”,RKLogLevelTrace);

NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
NSString *urlString = [NSString stringWithFormat:@"https://beta.stuff.com/"];
NSURL *url = [NSURL URLWithString:urlString];

//Object Mapping
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Info class]];
[mapping addAttributeMappingsFromDictionary:@{@"name": @"name", @"id": @"strenuusID", @"location_specialties.address": @"address", @"location_specialties.state": @"state", @"location_specialties.zip": @"zipcode", @"location_specialties.address_lines": @"address_lines"}];

NSString *pathPatternString = [NSString stringWithFormat:@"stuff/%@/stuff2/%@/", moreresultsVCSobj.projectNumber, moreresultsVCSobj.doctorStuff];

//Response Descriptor
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                                method:RKRequestMethodGET
                                                                                           pathPattern:pathPatternString
                                                                                               keyPath:@""
                                                                                           statusCodes:statusCodeSet];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:url];
[manager addResponseDescriptor:responseDescriptor];
[manager.HTTPClient setDefaultHeader:@"Auth-Token" value:moreresultsVCSobj.userAuthToken];

Info *info = [Info new];
[manager getObject:info path:pathPatternString parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
    NSLog(@"Mapping Result: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error)
{
    NSLog(@"Error: %@", error);
}];

这是我正在解析的JSON:

response.body=
{
"name": "Name, Generic N,   DDS",
"id": 123456,
"npi": "1234567890",
"hospitality": false,
"groupiness": [
    "Other"
],
"client_ids": null,
"has_comments": false,
"comment_count": 0,
"is_facility": false,
"location_specialties": [
    {
        "id": 123456,
        "address_id": 454345,
        "address": "1234 Rifle    Rd",
        "state": "Al",
        "zip": "43543",
        "address_lines": [
            "1234 Rifle Rd",
            "Generic Town, Al    43543",
            "111-222-    3344"
        ],
        "latitude": 21432234,
        "longitude": 432432,
        "is_validated": true,
        "providers_at_address_count": 2,
        "specialties": [
            {
                "name": "Dentists",
                "plans": [
                    {
                        "name": "Plan name",
                        "lists_specialty": true
                    },
                    {
                        "name": "Plane name 2",
                        "lists_specialty": true
                    },
                    {
                        "name": "Plan name 3",
                        "lists_specialty": true
                    }
                ]
            }
        ]
    },
    [
        {
            "id": 123456,
            "address_id": 454345,
            "address": "1234 Rifle      Rd",
            "state": "Al",
            "zip": "43543",
            "address_lines": [
                "1234 Rifle Rd",
                "Generic Town, Al    43543",
                "111-222-        3344"
            ],
            "latitude": 21432234,
            "longitude": 432432,
            "is_validated": true,
            "providers_at_address_count": 2,
            "specialties": [
                {
                    "name": "Dentists",
                    "plans": [
                        {
                            "name": "Plan name",
                            "lists_specialty": true
                        },
                        {
                            "name": "Plane name 2",
                            "lists_specialty": true
                        },
                        {
                            "name": "Plan name 3",
                            "lists_specialty": true
                        }
                    ]
                }
            ]
        },
        "hospital_detail": null,
        "tags": [

        ]
    }

已编辑:这是我的Info.h文件。我尝试检查映射等其他名称,但它们仍无效。

#import <Foundation/Foundation.h>

@interface Info : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *strenuusID;
@property (nonatomic, copy) NSString *npi;
@property (nonatomic, copy) NSArray *specialty_search_groups;
@property (nonatomic, copy) NSArray *address_lines;
@property (nonatomic, copy) NSArray *address;
@property (nonatomic, copy) NSString *state;
@property (nonatomic, copy) NSString *zipcode;
@property (nonatomic, copy) NSString *planName;

@end

1 个答案:

答案 0 :(得分:0)

您获得一个结果对象,因为您的JSON是一个字典。您的映射和响应描述符是根据单个对象编写的。

此映射:

@"location_specialties.address": @"address"

无效,因为location_specialties是一个数组,因此无法访问该地址。除非您的目标是数组变量,否则它可能会起作用。

您需要详细检查Info对象的内容,并在跟踪日志中查找问题。如果您仍然遇到问题,则需要提供有关Info对象本身的详细信息。