RestKit 0.20 Post Array Json issues

只愿长相守 提交于 2019-12-11 19:17:15

问题


This is the json that I need to post to services.

{
"deviceToken":"asdfasdfasdf",
"alarm": [
{
    "start" "8:30",
    "end": "9:30",
    "line": "156",
    "code": "xxxafsdfasdf", 
    "station": "asdfa",
    "stationLeft": 5,
    "available": true,
    "times": 2
}]
}

The response data just have one more field "id" in alarm:

    {
        "deviceToken":"asdfasdfasdf",
        "alarm": [
        {
           "id":1,
           "start" "8:30",
           "end": "9:30",
           "line": "156",
           "code": "xxxafsdfasdf", 
           "station": "asdfa",
           "stationLeft": 5,
           "available": true,
           "times": 2
        }]
     }

Then I define two objects: DeviceAlarm Object:

@interface DeviceAlarm : NSObject

@property(nonatomic, strong) NSMutableArray *alarm;

@property(nonatomic, copy) NSString *deviceToken;

@end

Alarm Object:

@interface Alarm : NSObject

@property(nonatomic, copy) NSNumber *id;

@property(nonatomic, copy) NSString *start;

@property(nonatomic, copy) NSString *end;

@property(nonatomic, copy) NSString *code;

@property(nonatomic, copy) NSString *line;

@property(nonatomic, copy) NSString *station;

@property(nonatomic, copy) NSNumber *stationLeft;

@property(nonatomic) BOOL available;

@property(nonatomic, copy) NSNumber *times;

@end

This is my code to post.

DeviceAlarm* devicealarm = [[DeviceAlarm alloc] init];

Alarm* alarm = [[Alarm alloc] init];
alarm.start = @"8:00";
alarm.end = @"9:30";
alarm.line = @"156";
alarm.code = @"fasdfasdf";
alarm.station = @"asdfas";
alarm.stationLeft = @1000;
alarm.available = true;
alarm.times = @1;

devicealarm.alarm = [NSArray arrayWithObjects:alarm , nil];
devicealarm.deviceToken = @"adsfasdfasdf";

RKObjectMapping *alarmMapping = [RKObjectMapping requestMapping];

[alarmMapping addAttributeMappingsFromArray:@[@"start",@"end",@"code",@"station", @"stationLeft",@"available",@"times",@"line"]];

RKObjectMapping *deviceMapping = [RKObjectMapping requestMapping];

RKRelationshipMapping *alarmRelationship = [RKRelationshipMapping
                                           relationshipMappingFromKeyPath:@"alarm"
                                           toKeyPath:@"alarm"
                                           withMapping:alarmMapping];


[deviceMapping addAttributeMappingsFromArray:@[@"deviceToken"]];

[deviceMapping addPropertyMapping:alarmRelationship];

NSString* path = @"/api/alarm/asdfasdf";
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:deviceMapping
                                                                               objectClass:[DeviceAlarm class]
                                                                               rootKeyPath:nil];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[DeviceAlarm DeviceAlarmResponseMapping]
                                                                                   pathPattern:nil
                                                                                       keyPath: nil
                                                                                   statusCodes:[NSIndexSet indexSetWithIndex:200]];


RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://127.0.0.1:5000/"]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];

[manager postObject:devicealarm path:path parameters:nil
                                    success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
                                        NSLog(@"Loading mapping result: %@", result);

                                    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                        RKLogError(@"Operation failed with error: %@", error);
                                    }];

Then I check the post data in services, I found restkit post an error array json. alarm dict lost.

    {
        "deviceToken":"asdfasdfasdf",
        "alarm": [
                  "8:30",
                  "9:30",
                  "156",
                  "xxxafsdfasdf", 
                  "asdfa",
                  5,
                  true,
                  2
         ]
     }

Please help me~ :)


回答1:


LOL, I have fixed my issues, I changed the field type from "NSMutableArray" to "NSSet" in DeviceAlarm Object, and then it works. I don't know why. Hope it can help us.



来源:https://stackoverflow.com/questions/16556090/restkit-0-20-post-array-json-issues

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