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
}
}
]
}
source
share