使用restkit将对象数组转换为json

时间:2014-04-07 08:11:28

标签: ios objective-c json restkit-0.20

我知道如何使用像

这样的restkit将对象转换为json
User* user = [User new];
user.firstName = @"Blake";
user.lastName = @"Watters";

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSDictionary class] ];
[mapping addAttributeMappingsFromArray:@[ @"firstName", @"lastName"] ];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping objectClass:[User class] rootKeyPath:@"user" method:RKRequestMethodAny];
NSError* error;
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:object requestDescriptor:requestDescriptor error:&error];

// Serialize the object to JSON
NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];

case1:如果它不仅仅是User对象,如果它像NSArray *用户那样;

case2:如果它不仅仅是User对象,如果它像NSArray *用户和User类有子类一样叫做课程,例如,如何让json发送到Web服务;

0 个答案:

没有答案