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

时间:2012-04-09 17:55:54

标签: 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"];

2 个答案:

答案 0 :(得分:2)

我找到了另一个解决方案。在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"}
    }

答案 1 :(得分:0)

您当然应该添加一个包含Customer属性的对象。

例如:

#import "Customer.h"

@interface SOCustomer : NSObject
@property (nonatomic, retain) Customer* customer;
@end

在此之后,您必须更改序列化。也许是这样的:

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]];

// added
RKObjectMapping *vocustomerSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[vocustomerSerializationMapping mapKeyPath:@"customer" toRelationship:@"customer" withMapping:customerSerializationMapping ];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:vocustomerSerializationMapping  forClass:[Customer class]];

我无法测试此代码,但我认为这是正确的方法。 只需找到如何正确序列化。

希望这可以帮到你。