如何发送JSON格式的input嵌套在NSDictionary中的值

如果input一个简单的非嵌套值的代码是:

$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 ,以在AFNetworking parameters AFNetworking其作为JSON对象发送。 如果我通过它NSMutableArray它不起作用。

你可以尝试下面的代码:

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

它会看起来像

 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" } ] } ] 

希望这会解决你的问题,

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

它的工作原理

 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"} ], }; 

否则,它会给你不好的要求。