Talha Ch Talha Ch - 3 months ago 12
JSON Question

How to send input in JSON form for nested values in NSDictionary

If the code for input a simple non-nested values is:

$values = array('input_values' => array('input_1' => 'Testing',
'input_2' => 'Testing',
'input_3' => 'Testing',));


and in
Obj-C
it will go as:

NSDictionary *params = @{@"input_1": @"Testing",
@"input_2": @"Testing",
@"input_3": @"Testing"};

NSMutableDictionary *modify = [NSMutableDictionary new];
[modify setObject:params forKey:@"input_values"];


Then how do we convert and send this nested code in 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'
)
,
),
);



UPDATE:
Based on the answers below if I implement this:

// 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]);
}];


It still gave me
400 Bad request
. As based on previous experience I need
NSMutableDictionary
of the above data in order to send it as JSON Object in
parameters
of
AFNetworking
. If I pass it
NSMutableArray
it doesn't work.

Answer

It works like

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

Otherwise it'll give you Bad request.

Comments