在Objective-C中使用RestKit发布复杂的json

时间:2016-08-05 22:03:36

标签: objective-c iphone arrays post restkit

我在使用RestKit发布json时遇到了问题,但我找不到我做错了什么。 请求json示例是:

{
  "Name": "sample string 1", 
  "Interests": {
    "Array": [
      1,
      2,
      3
    ]
  }
}

我的代码:

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
    [requestMapping addAttributeMappingsFromDictionary:@{@"Name": @"Name"}];

    RKObjectMapping *interestsMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];

    RKObjectMapping *interestIDMapping = [RKObjectMapping mappingForClass:[KLInterest class]];
    [interestIDMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"InterestID"]];

    [interestsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Array" toKeyPath:@"Array" withMapping:interestIDMapping]];


    [requestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Interests" toKeyPath:@"Interests" withMapping:interestsMapping]];

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[NSMutableDictionary class] rootKeyPath:nil method:RKRequestMethodPOST];
    [self.objectManager addRequestDescriptor:requestDescriptor];


    NSDictionary *data = [business toDictionary];

    [self sendRequest:@"api/Business/Create" Method:@"POST" Data:data withCallback:^(BOOL success, NSDictionary response, NSError error) {
        callback(success, response, error);
    }];

KLInterestID.h

@interface KLInterestID : NSObject
@property (nonatomic) NSNumber *InterestID;
@end

请让我知道我做错了什么。我不熟悉relationshipMappingFromKeyPathattributeMappingFromKeyPath:nil函数。 我不确定如何发送int数组。 感谢

0 个答案:

没有答案