AFNetworking POST Method with Nested NSDictionary

I am using AF-networking 2. I want to send Nested NSMutableDictionaryto the server. This is the method I use.

NSMutableArray *arrayRecordSet =[NSMutableArray array];
    for (id contact in _contactGroupArr) {

        ContactGroup *contactGroup=contact;

        NSMutableDictionary *dictGroupDetails=[NSMutableDictionary dictionary];
        [dictGroupDetails setObject:contactGroup.guid forKey:@"guid"];
        [dictGroupDetails setObject:contactGroup.name forKey:@"name"];
        [dictGroupDetails setObject:@"" forKey:@"description"];
        [dictGroupDetails setObject:contactGroup.status forKey:@"isDeleted"];

        NSMutableDictionary *dictContactGroup=[[NSMutableDictionary alloc]init];
        [dictContactGroup setObject:dictGroupDetails forKey:@"contact_group"];

        [arrayRecordSet addObject:dictContactGroup];

    }

    NSMutableDictionary *dictSyncData=[[NSMutableDictionary alloc]init];
    [dictSyncData setObject:arrayRecordSet forKey:@"recordset"];
    [dictSyncData setValue:@"2014-06-30 04:47:45" forKey:@"last_sync_date"];

    NSString *getPath = [NSString stringWithFormat:API_SYNC_CONATCTGROUP];

    [self POST:getPath parameters:dictSyncData
       success:^(NSURLSessionDataTask *task, id responseObject) {

           NSLog(@"%@",responseObject);
       }
       failure:^(NSURLSessionDataTask *task, NSError *error) {
           NSLog(@"%@",error);
       }];

But a server expecting Json format,

Array
(
    [last_sync_date] => 2014-06-30 04:47:45
    [recordset] => Array
        (
            [0] => Array
                (
                    [contact_group] => Array
                        (
                            [guid] => y37845y8y
                            [name] => Family
                            [description] => Family members
                            [isDeleted] => 0
                        )

                )

            [1] => Array
                (
                    [contact_group] => Array
                        (
                            [guid] => gt45tergh4
                            [name] => Office
                            [description] => Office members
                            [isDeleted] => 0
                        )

                )

        )

)

But my code sends this wrong format

Array
(
    [last_sync_date] => 2014-06-30 04:47:45
    [recordset] => Array
        (
            [0] => Array
                (
                    [contact_group] => Array
                        (
                            [description] => 
                        )

                )

            [1] => Array
                (
                    [contact_group] => Array
                        (
                            [guid] => 8D9763F6-38EF-49C7-860D-62759D77A779
                        )

                )

            [2] => Array
                (
                    [contact_group] => Array
                        (
                            [isDeleted] => 1
                        )

                )

            [3] => Array
                (
                    [contact_group] => Array
                        (
                            [name] => dfhdfhfd
                        )

                )

            [4] => Array
                (
                    [contact_group] => Array
                        (
                            [description] => 
                        )

                )

            [5] => Array
                (
                    [contact_group] => Array
                        (
                            [guid] => B9AC3F61-6F35-4447-87CE-6399E863C84A
                        )

                )

            [6] => Array
                (
                    [contact_group] => Array
                        (
                            [isDeleted] => 1
                        )

                )

            [7] => Array
                (
                    [contact_group] => Array
                        (
                            [name] => eretert
                        )

                )

            [8] => Array
                (
                    [contact_group] => Array
                        (
                            [description] => 
                        )

                )

            [9] => Array
                (
                    [contact_group] => Array
                        (
                            [guid] => 8D651A62-ABDE-4062-96EF-0922768008FA
                        )

                )

            [10] => Array
                (
                    [contact_group] => Array
                        (
                            [isDeleted] => 1
                        )

                )

            [11] => Array
                (
                    [contact_group] => Array
                        (
                            [name] => gghfgh
                        )

                )

        )

)

But I checked NSMutableDictionary. Right. Because I manually create a string NSJson. It will give the correct format. I am trying to create the wrong json format.

I am doing my best. But still I have a problem.

EDIT

Server is expecting json

{  

    "last_sync_date": "2014-06-30 04:47:45",

    "recordset": [

        {

            "contact_group": {

                "guid": "y37845y8y",

                "name": "Family",        

                "description": "Family members",              

                "isDeleted": 0        

            } 

        },

        {

            "contact_group": { 

                "guid": "gt45tergh4",        

                "name": "Office",        

                "description": "Office members",              

                "isDeleted": 0        

            } 

        } 

    ]

}
+4
source share
2 answers

it looks like you should warp the dictionary contact_groupin another dictionary. Try something like this.

int index = 0;

for (id contact in _contactGroupArr) {

        ContactGroup *contactGroup=contact;

        //level 1
        NSMutableDictionary *dictGroupDetails=[NSMutableDictionary dictionary];
        [dictGroupDetails setObject:contactGroup.guid forKey:@"guid"];
        [dictGroupDetails setObject:contactGroup.name forKey:@"name"];
        [dictGroupDetails setObject:@"" forKey:@"description"];
        [dictGroupDetails setObject:contactGroup.status forKey:@"isDeleted"];

        //level 2
        NSMutableDictionary *dictContactGroup=[[NSMutableDictionary alloc]init];
        [dictContactGroup setObject:dictGroupDetails forKey:@"contact_group"];

        //level 3
        NSMutableDictionary *dictArry=[[NSMutableDictionary alloc]init];
        [dictArry setObject:dictContactGroup forKey:[NSString stringWithFormat:@"%d",index]];

        [arrayRecordSet addObject:dictArry];

        index ++;

    }

, 3, dictContactGroup NSMutableDictionary NSMutableDictionary dictArry, - JSON .

    [0] => Array
    (
       [contact_group] => Array
         (
            [guid] => y37845y8y
            [name] => Family
            [description] => Family members
            [isDeleted] => 0
         )

    )
+1

, :

NSMutableDictionary *dictContactGroup=[[NSMutableDictionary alloc]init];
[dictContactGroup setObject:dictGroupDetails forKey:@"contact_group"];

, :

dictGroupDetails

, dictContactGroup, :

NSMutableDictionary *dictContactGroup=[[NSMutableDictionary alloc]init]; 

NSArray *wrapperArray = @[dictGroupDetails];

[dictContactGroup setObject:wrapperArray forKey:@"contact_group"];

, ?

1

, , arrRecordSet .

, dictContactGroup wrapperArray:

NSMutableDictionary *dictContactGroup=[[NSMutableDictionary alloc]init]; 

NSArray *wrapperArray = @[dictGroupDetails];

[dictContactGroup setObject:wrapperArray forKey:@"contact_group"];

// notice the extra @[ ]  wrapped around "dictContactGroup"
[arrayRecordSet addObject:@[dictContactGroup]];

2

, " " , , , .

:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSMutableArray *arrRecordSet = [[NSMutableArray alloc] init];

    NSArray *arrContactGroups = @[@0, @1, @2, @3, @4, @5];

    for(int i = 0; i < arrContactGroups.count; i++)
    {
        NSMutableDictionary *dictGroupDetails=[NSMutableDictionary dictionary];
        [dictGroupDetails setObject:@"1234" forKey:@"guid"];
        [dictGroupDetails setObject:@"group name" forKey:@"name"];
        [dictGroupDetails setObject:@"group description" forKey:@"description"];
        [dictGroupDetails setObject:@"not deleted" forKey:@"isDeleted"];

        NSMutableDictionary *dictContactGroup=[[NSMutableDictionary alloc]init];
        [dictContactGroup setObject:@[dictGroupDetails] forKey:@"contact_group"];

        [arrRecordSet addObject:@[dictContactGroup]];
    }

    NSMutableDictionary *dictSyncData=[[NSMutableDictionary alloc]init];
    [dictSyncData setObject:arrRecordSet forKey:@"recordset"];
    [dictSyncData setValue:@"2014-06-30 04:47:45" forKey:@"last_sync_date"];

    NSLog(@"dictSyncData = \n\n%@", dictSyncData);

}

:

{
    "last_sync_date" = "2014-06-30 04:47:45";
    recordset =     (
                (
                        {
                "contact_group" =                 (
                                        {
                        description = "group description";
                        guid = 1234;
                        isDeleted = "not deleted";
                        name = "group name";
                    }
                );
            }
        ),
                (
                        {
                "contact_group" =                 (
                                        {
                        description = "group description";
                        guid = 1234;
                        isDeleted = "not deleted";
                        name = "group name";
                    }
                );
            }
        ),
                (
                        {
                "contact_group" =                 (
                                        {
                        description = "group description";
                        guid = 1234;
                        isDeleted = "not deleted";
                        name = "group name";
                    }
                );
            }
        ),
                (
                        {
                "contact_group" =                 (
                                        {
                        description = "group description";
                        guid = 1234;
                        isDeleted = "not deleted";
                        name = "group name";
                    }
                );
            }
        ),
                (
                        {
                "contact_group" =                 (
                                        {
                        description = "group description";
                        guid = 1234;
                        isDeleted = "not deleted";
                        name = "group name";
                    }
                );
            }
        ),
                (
                        {
                "contact_group" =                 (
                                        {
                        description = "group description";
                        guid = 1234;
                        isDeleted = "not deleted";
                        name = "group name";
                    }
                );
            }
        )
    );
}

, , - .

, , , ?

0

All Articles