如果用于输入简单非嵌套值的代码是:
$values = array('input_values' => array('input_1' => 'Testing',
'input_2' => 'Testing',
'input_3' => 'Testing',));
并在Obj-C
中将其视为:
NSDictionary *params = @{@"input_1": @"Testing",
@"input_2": @"Testing",
@"input_3": @"Testing"};
NSMutableDictionary *modify = [NSMutableDictionary new];
[modify setObject:params forKey:@"input_values"];
那么我们如何在obj-C中转换和发送这个嵌套代码?
$forms = array(
array(
'title' => 'API Generated 10',
'description' => 'This is the description',
'labelPlacement' => 'top_label',
'button' => array(
'type' => 'text'
),
'confirmations' => array(
array(
'id' => 0,
'name' => 'Default Confirmation',
'type' => 'message',
'message' => 'Thanks for contacting us! We will get in touch with you shortly.',
'isDefault' => true,
),
),
'fields' => array(
array(
'id' => '1',
'label' => 'My Text',
'type' => 'text'
)
array(
'id' => '2',
'label' => 'My Text 2',
'type' => 'text'
)
,
),
);
更新:
根据以下答案,如果我实现这个:
// Initially Crate one Main Dictionary for Save
NSMutableDictionary *modify = [NSMutableDictionary new];
NSMutableArray *ParamArray = [NSMutableArray array];
NSDictionary *type = @{@"type": @"type" };
NSDictionary *original = @{@"title": @"API Generated 10",
@"description": @"This is the description for the form generated by the API",
@"labelPlacement": @"top_label",
@"button":type
};
// NSArray * svalues = original.mutableCopy;
modify = original.mutableCopy;
// store all Fields in one array
NSDictionary *fieldsparams = @{@"id": @"1",
@"label": @"My Text",
@"type": @"text"};
NSArray * fieldsvalues = fieldsparams.mutableCopy;
[modify setObject:fieldsvalues forKey:@"fields"];
NSDictionary *confirmationssparams = @{@"id": @"0",
@"name": @"Default Confirmation",
@"type": @"message",
@"message":@"Thanks for contacting us! We will get in touch with you shortly.",
@"isDefault":@YES
};
NSArray * confirmationsvalues = confirmationssparams.mutableCopy;
[modify setObject:confirmationsvalues forKey:@"confirmations"];
[ParamArray addObject:modify];
NSString *escapedPath = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//1
AFHTTPSessionManager* manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:escapedPath parameters:modify progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
NSLog(@"json == %@",json);
}failure:^(NSURLSessionTask *operation, NSError *error)
{
NSLog(@"%@",[error localizedDescription]);
}];
它仍然给了我400 Bad request
。根据以前的经验,我需要NSMutableDictionary
上述数据,以便在parameters
AFNetworking
中将其作为JSON对象发送。如果我通过它NSMutableArray
它就不起作用。
答案 0 :(得分:1)
您可以尝试以下代码:
NSArray *fieldsArr = //your fieldsArr
NSArray *confirmationsArr = //your confirmationsArr
NSArray *buttonArr = //your buttonArr
NSMutableDictionary *modify = [NSMutableDictionary new];
[modify setValue:@"Titlr" forKey:@"title"];
[modify setValue:@"Disciption" forKey:@"description"];
[modify setValue:@"lablPlacement" forKey:@"labelPlacement"];
[modify setObject:buttonArr forKey:@"button"];
[modify setObject:fieldsArr forKey:@"fields"];
[modify setObject:confirmationsArr forKey:@"confirmations"];
NSMutableArray *mainArr = [NSMutableArray new];
[mainArr addObject:modify];
//Make request
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"[JSON SERVER"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mainArr options:NSJSONWritingPrettyPrinted error:&error];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[postDataTask resume];
答案 1 :(得分:0)
看起来像
NSArray * params = @[
@{
@"title" : @"API Generated 10",
@"description" : @"This is the description",
@"labelPlacement" : @"top_label",
@"button" : @{
@"type" : @"text"
},
@"confirmations" : @[
@{
@"id" : @0,
@"name" : @"Default Confirmation",
@"type" : @"message",
@"message" : @"Thanks for contacting us! We will get in touch with you shortly.",
@"isDefault": @"true",
}
],
@"fields" : @[
@{
@"id" : @1,
@"label" : @"My Text",
@"type" : @"text"
},
@{
@"id" : @2,
@"label" : @"My Text 2",
@"type" : @"text"
}
]
}
]
答案 2 :(得分:0)
希望这能解决您的问题 -
NSMutableArray *MainArray = [[NSMutableArray alloc]init];
//first dictionary
NSDictionary *subDic1 = [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects:
@"API Generated 10",
@"This is the description",
@"top_label",
@{@"type":@"text"},
nil]
forKeys:[NSArray arrayWithObjects:@"title",@"description1",@"labelPlacement",@"button", nil]];
//second dictionary
NSArray *arr1 = [NSArray arrayWithObjects:
@{
@"id":@0,
@"name":@"Default Confirmation",
@"type":@"message",
@"message":@"Thanks for contacting us! We will get in touch with you shortly.",
@"isDefault":@true
},
nil] ;
NSDictionary *subDic2 = [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects:arr1, nil] forKeys:[NSArray arrayWithObjects:@"confirmations", nil]];
//third dictionary
NSArray *arr2 = [NSArray arrayWithObjects:
@{@"id":@1,@"label":@"My Text",@"type":@"text"},
@{@"id":@2,@"label":@"My Text2",@"type":@"text"},
nil] ;
NSDictionary *subDic3 = [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects:arr2, nil] forKeys:[NSArray arrayWithObjects:@"fields", nil]];
//now merge all dictionaries in one main array
[MainArray addObject:subDic1];
[MainArray addObject:subDic2];
[MainArray addObject:subDic3];
答案 3 :(得分:0)
就像
一样NSDictionary *fullData = @{
@"title": @"API Generated",
@"description": @"This is the description for the form generated by the API",
@"labelPlacement": @"top_label",
@"button":@{@"type": @"text"},
@"confirmations": @{
@"id": @0,
@"name": @"Default Confirmation",
@"type": @"message",
@"message": @"Thanks for contacting us! We will get in touch with you shortly.",
@"isDefault": @true,
},
@"fields": @[
@{
@"id":@1,
@"label":@"My Text",
@"type":@"text"},
@{
@"id":@2,
@"label":@"My Text 2",
@"type":@"text"}
],
};
否则它会给你错误的请求。