【问题标题】:Passing json to request body using restkit in ios在ios中使用restkit将json传递给请求正文
【发布时间】:2014-08-16 20:50:54
【问题描述】:

我在我的应用程序中使用 restkit 从我的网络服务发布/获取任何内容。 我有一个嵌套的 json,我想将它发布到我的 API 的请求正文中。我使用以下代码将 json 传递给我的服务的请求正文,但是当我运行应用程序时,它在我的请求正文中返回 null。

这是我要传递给请求正文的嵌套 json

{
    amount =     {
        currency = INR;
        value = 1;
    };
    merchantAccessKey = UDNIRNIRFBNRIRN;
    merchantTxnId = sssscdcdcdEDFF;
    notifyUrl = "";
    paymentToken =     {
        paymentMode =         {
            cvv = 968;
            expiry = “11/20”;
            holder = AXIS;
            number = xxxxxxxxxxxx;
            scheme = asxsc;
            type = debit;
        };
        type = paymentOptionToken;
    };
    requestSignature = abgd9456fef7b3f9023232734706;
    returnUrl = "https://www.example.com/";
    userDetails =     {
        address =         {
            city = "";
            country = "";
            state = "";
            street1 = "";
            street2 = "";
            zip = "";
        };
        email = “abc@gmail.com”;
        firstName = abc;
        lastName = xyz;
        mobileNo = 1234567890;
    };
}

这是我将 json 发送到 API 的代码。

- (void)ComplexRequestMapping{
 RKObjectMapping* paymentamount = [RKObjectMapping requestMapping];

  RKObjectMapping* amountMapping =
      [RKObjectMapping mappingForClass:[Amount class]];
  [amountMapping addAttributeMappingsFromDictionary:
                     @{@"currency" : @"currency", @"value" : @"value"}];
  [paymentamount
      addPropertyMapping:[RKRelationshipMapping
                             relationshipMappingFromKeyPath:@"amount"
                                                  toKeyPath:@"amount"
                                                withMapping:amountMapping]];

  RKObjectMapping* userdetailsMapping =
      [RKObjectMapping mappingForClass:[UserDetails class]];
  [userdetailsMapping
      addAttributeMappingsFromDictionary:@{
                                           @"email" : @"email",
                                           @"firstName" : @"firstName",
                                           @"lastName" : @"lastName",
                                           @"mobileNo" : @"mobileNo"
                                         }];
  RKObjectMapping* addressMapping =
      [RKObjectMapping mappingForClass:[UserAddress class]];
  [addressMapping addAttributeMappingsFromDictionary:@{
                                                       @"city" : @"city",
                                                       @"country" : @"country",
                                                       @"state" : @"state",
                                                       @"street1" : @"street1",
                                                       @"street2" : @"street2",
                                                       @"zip" : @"zip"
                                                     }];
  [addressMapping
      addPropertyMapping:
          [RKRelationshipMapping
              relationshipMappingFromKeyPath:@"address"
                                   toKeyPath:@"address"
                                 withMapping:[userdetailsMapping
                                                     inverseMapping]]];

  RKObjectMapping* paymentTokenMapping =
      [RKObjectMapping mappingForClass:[CTSPaymentToken class]];
  [paymentTokenMapping addAttributeMappingsFromDictionary:@{@"type" : @"type"}];

  RKObjectMapping* paymentOptionsMapping =
      [RKObjectMapping mappingForClass:[CTSPaymentOption class]];
  [paymentOptionsMapping
      addAttributeMappingsFromDictionary:@{
                                           @"cvv" : @"cvv",
                                           @"expiry" : @"expiry",
                                           @"holder" : @"holder",
                                           @"number" : @"number",
                                           @"scheme" : @"scheme",
                                           @"type" : @"type"
                                         }];
  [paymentOptionsMapping
      addPropertyMapping:
          [RKRelationshipMapping
              relationshipMappingFromKeyPath:@"paymentMode"
                                   toKeyPath:@"paymentMode"
                                 withMapping:[paymentTokenMapping
                                                     inverseMapping]]];
  RKRequestDescriptor* requestDes = [RKRequestDescriptor
      requestDescriptorWithMapping:amountMapping
                       objectClass:[CTSPaymentRequest class]
                       rootKeyPath:nil
                            method:RKRequestMethodPOST];
  [objectManager addRequestDescriptor:requestDes];

}

【问题讨论】:

  • 试试简单的 json 数据。

标签: ios objective-c json restkit


【解决方案1】:
Try with simple json 
{
   title = Test;
   body = Sample text;
   auther = Test author;
}
// by using NSJson class you can convert json data to object.Here i manually create my own object

    yourObject *object = [yourObject new];
    object.title = @“Test”;
    object.body = @“Sample text”;
    object.author = @“Test auther”;

    RKResponseDescriptor * yourObjectDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"/test" keyPath:@"test" statusCodes:statusCodes];
    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
    [requestMapping addAttributeMappingsFromArray:@[@"title", @"author", @"body"]];

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[yourObject class] rootKeyPath:@“test” method:RKRequestMethodAny];

    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:BASE_URL];
    [manager addRequestDescriptor:requestDescriptor];

    // POST to create
    [manager postObject:object path:@“/test” parameters:nil success:nil failure:nil];

【讨论】:

    【解决方案2】:

    你没有说出了什么问题,但我猜你在尝试发送对请求映射的抱怨时会崩溃......

    创建请求映射时,目标类必须是可变字典。这就是 requestMappinginverseMapping 为您创建的内容。

    目前,您似乎在需要保持一致的地方随机组合响应和请求样式映射。要么创建响应映射,然后逆向创建请求映射,要么创建并关联多个请求映射部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 1970-01-01
      相关资源
      最近更新 更多