RestKit,PUT复杂的JSON到服务器

时间:2014-06-22 05:49:56

标签: json restkit restkit-0.20

请求json我想要PUT到服务器

"type": "payment",
"paymentOptions": [
    {
        "type": "netbanking",
        "name": "NetBanking - AXIS Bank",
        "expiryDate": null,
        "bank": "AXIS Bank"
    }
]

用于映射的类

@interface CTSPaymentDetailUpdate : NSObject
@property(nonatomic, strong, readonly) NSString* type;
@property(nonatomic, strong, readonly) NSMutableArray* paymentOptions;

@interface CTSPaymentOption : NSObject
@property(nonatomic, strong) NSString* type, *ownerName, *number, *bankName, *expiryDate;

映射代码

  #define MLC_PROFILE_UPDATE_PAYMENT_REQUEST_MAPPING \
@{                                               \
@"type" : @"type",                             \
@"bank" : @"bankName",                         \
@"owner" : @"ownerName",                       \
@"number" : @"number",                         \
@"expiryDate" : @"expiryDate",                 \
@"name" : @"cardName"                          \
}

  RKObjectMapping* paymentOptionMapping =
  [RKObjectMapping mappingForClass:[CTSPaymentOption class]];
  [paymentOptionMapping addAttributeMappingsFromDictionary:
                        MLC_PROFILE_UPDATE_PAYMENT_REQUEST_MAPPING];

  RKObjectMapping* paymentDetailReq = [RKObjectMapping requestMapping];
  [paymentDetailReq addAttributeMappingsFromDictionary:@{ @"type" : @"type" }];
  [paymentDetailReq
  addPropertyMapping:
      [RKRelationshipMapping
          relationshipMappingFromKeyPath:@"paymentOptions"
                               toKeyPath:@"paymentOptions"
                             withMapping:paymentOptionMapping]];

  RKRequestDescriptor* requestDes = [RKRequestDescriptor
  requestDescriptorWithMapping:paymentDetailReq
                   objectClass:[CTSPaymentDetailUpdate class]
                   rootKeyPath:nil
                        method:[self getHTTPMethodFor:PUT]];
  [objectManager addRequestDescriptor:requestDes];

一旦我使用以下内容将对象实际放入服务器

[objectManager putObject:object
  path:path
  parameters:queryParams
  success:^(RKObjectRequestOperation* operation,
            RKMappingResult* mappingResult) {
//code
}
failure:^(RKObjectRequestOperation* operation, NSError* error) {
    //code  
  }];

我在这一行得到了NSUnknownKeyException 2014-06-22 11:12:22.587 RestFulltester[1727:60b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CTSPaymentOption 0x10bf2ae80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key owner.'

我认为我制作映射的方式有问题,但我无法理解,也有任何方法可以测试此代码预先生成的json

2 个答案:

答案 0 :(得分:1)

这一行:

withMapping:paymentOptionMapping]];

创建关系时,映射应为:

withMapping:[paymentOptionMapping inverseMapping]]];

答案 1 :(得分:0)

您可以尝试这样:

[session.objectManager requestWithObject:object 
                                  method:RKRequestMethodPUT 
                                    path:queryPath
                              parameters:params]