2012-04-09 5 views
2

私は、ムックipadアプリケーションから私のレールアプリケーションに作成アクションを投稿するためにrestkitを使用しています。私は属性のマッピングを作成します。jsonがRestKitでpostObjectを使用して送信するカスタムの仕方は?

- (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を使用しようとしましたが、私はあなたが使用することもでき

EDIT

を失敗しました:あなたは確かに顧客の属性が含まれている1つの以上のオブジェクトを、追加する必要があります

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

答えて

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"} 
    } 
+0

非常に素晴らしいソリューション; P – Beber

+0

はあなたに感謝します!私はそれが有用だと思った – Sebastien

0

。 exempleについては

:この後

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

私はこのコードをテストすることはできませんが、正しい方法だと思います。 正しくシリアル化する方法を見つけるだけです。

希望すると、これが役立ちます。

関連する問題