我在我的应用程序中使用restkit从我的webservice发布/获取任何内容。 我有一个嵌套的json,我想发布到我的API的请求主体。我使用以下代码将json传递给我的服务的请求体,但是当我运行应用程序时,它在我的请求体中返回null。
这是我想要传递给请求正文的嵌套json
{
amount = {
currency = INR;
value = 1;
};
merchantAccessKey = UDNIRNIRFBNRIRN;
merchantTxnId = sssscdcdcdEDFF;
notifyUrl = "";
paymentToken = {
paymentMode = {
cvv = 968;
expiry = “11/20”;
holder = AXIS;
number = xxxxxxxxxxxx;
scheme = asxsc;
type = debit;
};
type = paymentOptionToken;
};
requestSignature = abgd9456fef7b3f9023232734706;
returnUrl = "https://www.example.com/";
userDetails = {
address = {
city = "";
country = "";
state = "";
street1 = "";
street2 = "";
zip = "";
};
email = “abc@gmail.com”;
firstName = abc;
lastName = xyz;
mobileNo = 1234567890;
};
}
这是我将json发送给API的代码。
- (void)ComplexRequestMapping{
RKObjectMapping* paymentamount = [RKObjectMapping requestMapping];
RKObjectMapping* amountMapping =
[RKObjectMapping mappingForClass:[Amount class]];
[amountMapping addAttributeMappingsFromDictionary:
@{@"currency" : @"currency", @"value" : @"value"}];
[paymentamount
addPropertyMapping:[RKRelationshipMapping
relationshipMappingFromKeyPath:@"amount"
toKeyPath:@"amount"
withMapping:amountMapping]];
RKObjectMapping* userdetailsMapping =
[RKObjectMapping mappingForClass:[UserDetails class]];
[userdetailsMapping
addAttributeMappingsFromDictionary:@{
@"email" : @"email",
@"firstName" : @"firstName",
@"lastName" : @"lastName",
@"mobileNo" : @"mobileNo"
}];
RKObjectMapping* addressMapping =
[RKObjectMapping mappingForClass:[UserAddress class]];
[addressMapping addAttributeMappingsFromDictionary:@{
@"city" : @"city",
@"country" : @"country",
@"state" : @"state",
@"street1" : @"street1",
@"street2" : @"street2",
@"zip" : @"zip"
}];
[addressMapping
addPropertyMapping:
[RKRelationshipMapping
relationshipMappingFromKeyPath:@"address"
toKeyPath:@"address"
withMapping:[userdetailsMapping
inverseMapping]]];
RKObjectMapping* paymentTokenMapping =
[RKObjectMapping mappingForClass:[CTSPaymentToken class]];
[paymentTokenMapping addAttributeMappingsFromDictionary:@{@"type" : @"type"}];
RKObjectMapping* paymentOptionsMapping =
[RKObjectMapping mappingForClass:[CTSPaymentOption class]];
[paymentOptionsMapping
addAttributeMappingsFromDictionary:@{
@"cvv" : @"cvv",
@"expiry" : @"expiry",
@"holder" : @"holder",
@"number" : @"number",
@"scheme" : @"scheme",
@"type" : @"type"
}];
[paymentOptionsMapping
addPropertyMapping:
[RKRelationshipMapping
relationshipMappingFromKeyPath:@"paymentMode"
toKeyPath:@"paymentMode"
withMapping:[paymentTokenMapping
inverseMapping]]];
RKRequestDescriptor* requestDes = [RKRequestDescriptor
requestDescriptorWithMapping:amountMapping
objectClass:[CTSPaymentRequest class]
rootKeyPath:nil
method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:requestDes];
}
答案 0 :(得分:0)
Try with simple json
{
title = Test;
body = Sample text;
auther = Test author;
}
// by using NSJson class you can convert json data to object.Here i manually create my own object
yourObject *object = [yourObject new];
object.title = @“Test”;
object.body = @“Sample text”;
object.author = @“Test auther”;
RKResponseDescriptor * yourObjectDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"/test" keyPath:@"test" statusCodes:statusCodes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:@[@"title", @"author", @"body"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[yourObject class] rootKeyPath:@“test” method:RKRequestMethodAny];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:BASE_URL];
[manager addRequestDescriptor:requestDescriptor];
// POST to create
[manager postObject:object path:@“/test” parameters:nil success:nil failure:nil];
答案 1 :(得分:0)
你没有说出了什么问题,但是当你试图发送抱怨请求映射时我猜你会崩溃...
创建请求映射时,目标类必须是可变字典。这是requestMapping
和inverseMapping
为您创建的内容。
目前,您似乎随机组合了响应和请求样式映射,您需要保持一致。创建响应映射,然后使用反向创建请求映射,或创建并关联多个请求映射部分。