English 中文(简体)
如何按照惯例,用员额派遣json 反对:
原标题:How to custom the json send using postObject with RestKit?

我利用休息室把制造行动张贴到我的铁路申请中。 我为特性绘制地图:

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

i 发出请求:

    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,但一经失败

http://www.ohchr.org。

也可以使用:

[[RKObjectManager sharedManager].mappingProvider registerMapping:customerSerializationMapping withRootKeyPath:@"customer"];
最佳回答

我找到了另一种解决办法。 在加注图中,我写成一个阵列:

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

这使我感到羞耻:

  { "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"}
    }
问题回答

你当然应该增加一个目标,其中包括客户归属。

例:

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

I can not test this code, but I think it s the right way. Just find how to serialize correctly.

希望能够帮助你们。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签