2

私は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"];
4

2 に答える 2

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"}
    }
于 2012-04-10T08:55:39.960 に答える
0

Customer 属性を含むオブジェクトをもう 1 つ追加する必要があります。

例えば ​​:

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

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

これがあなたを助けることを願っています。

于 2012-04-09T19:16:29.320 に答える