objective-c - 如何使用带有 RestKit 的 postObject 自定义 json 发送?

标签 objective-c restkit

我正在使用restkit 将创建操作从mu ipad 应用程序发布到我的rails 应用程序。我为属性创建映射:

- (void) initCustomerMapping
{
    RKObjectMapping *customerSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]]; 
    [customerSerializationMapping mapKeyPath:@"id" toAttribute:@"id"]; 
    [customerSerializationMapping mapKeyPath:@"lastname" toAttribute:@"lastname"]; 
    [customerSerializationMapping mapKeyPath:@"firstname" toAttribute:@"firstname"]; 
    [customerSerializationMapping mapKeyPath:@"house_name" toAttribute:@"house_name"]; 
    [customerSerializationMapping mapKeyPath:@"sci" toAttribute:@"sci"]; 
    [customerSerializationMapping mapKeyPath:@"water_used" toAttribute:@"water_used"]; 
    [customerSerializationMapping mapKeyPath:@"address_line_1" toAttribute:@"address_line_1"]; 
    [customerSerializationMapping mapKeyPath:@"address_line_1" toAttribute:@"address_line_2"]; 
    [customerSerializationMapping mapKeyPath:@"postal_code" toAttribute:@"postal_code"]; 
    [customerSerializationMapping mapKeyPath:@"city" toAttribute:@"city"];

    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:customerSerializationMapping forClass:[Customer class]]; 
}

我发送请求:

    Customer* customer = [[Customer alloc] init]; 
    customer.lastname = lastname.text;
    customer.firstname = firstname.text;
    customer.house_name = house_name.text;
    customer.sci = sci.text;
    customer.water_used = water_used.text;
    customer.address_line_1 = address_line_1.text;
    customer.address_line_2 = address_line_2.text;
    customer.postal_code = postal_code.text;
    customer.city = city.text;

    [[RKObjectManager sharedManager] postObject:customer delegate:self];

但是 json 发送看起来像:

 {"water_used"=>"710", "house_name"=>"test", "address_line_1"=>"test", "city"=>"test", "sci"=>"546", "firstname"=>"test", "lastname"=>"test", "address_line_2"=>"test", "postal_code"=>"75896"}

我想得到:

   { "customer" => 
       {"water_used"=>"710", "house_name"=>"test", "address_line_1"=>"test", "city"=>"test", "sci"=>"546", "firstname"=>"test", "lastname"=>"test", "address_line_2"=>"test", "postal_code"=>"75896"}
    }

我该如何配置?我尝试使用 forKey 但失败了

编辑

您还可以使用:

[[RKObjectManager sharedManager].mappingProvider registerMapping:customerSerializationMapping withRootKeyPath:@"customer"];

最佳答案

我找到了另一个解决方案。在toAttribute的字符串中,我将其写为数组:

- (void) initCustomerMapping
{
    RKObjectMapping *customerSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]]; 
    [customerSerializationMapping mapKeyPath:@"id" toAttribute:@"customer[id]"]; 
    [customerSerializationMapping mapKeyPath:@"lastname" toAttribute:@"customer[lastname]"]; 
    [customerSerializationMapping mapKeyPath:@"firstname" toAttribute:@"customer[firstname]"]; 
    [customerSerializationMapping mapKeyPath:@"house_name" toAttribute:@"customer[house_name]"]; 
    [customerSerializationMapping mapKeyPath:@"sci" toAttribute:@"customer[sci]"]; 
    [customerSerializationMapping mapKeyPath:@"water_used" toAttribute:@"customer[water_used]"]; 
    [customerSerializationMapping mapKeyPath:@"address_line_1" toAttribute:@"customer[address_line_1]"]; 
    [customerSerializationMapping mapKeyPath:@"address_line_1" toAttribute:@"customer[address_line_2]"]; 
    [customerSerializationMapping mapKeyPath:@"postal_code" toAttribute:@"customer[postal_code]"]; 
    [customerSerializationMapping mapKeyPath:@"city" toAttribute:@"customer[city]"];

    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:customerSerializationMapping forClass:[Customer class]]; 
}

它给了我我的 json :

  { "customer" => 
       {"water_used"=>"710", "house_name"=>"test", "address_line_1"=>"test", "city"=>"test", "sci"=>"546", "firstname"=>"test", "lastname"=>"test", "address_line_2"=>"test", "postal_code"=>"75896"}
    }

关于objective-c - 如何使用带有 RestKit 的 postObject 自定义 json 发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10077450/

相关文章:

iphone - 我将如何将一个 View Controller 推送到另一个没有 nib 文件的 View Controller ?

ios - 如何在执行 CIFilter 时在 iOS UIImageView 中保留图像纵横比,如 CISepiaTone

objective-c - 无法关闭游戏中心排行榜

ios - 使用restkit序列化/非序列化对象的干净方法?

java - 安卓中的 RestKit?

ios - RestKit:同步发布多个对象

ios - 在 Objective-C 中以编程方式将文本置于图像上

html - 使用 TFHpple 解析网页

ios - 带休息套件的 POST

ios - 带有 XML 的 RestKit - 将代码转换为 0.20