我正在使用AF-networking 2.我想将嵌套NSMutableDictionary
发送到服务器。这是我正在使用的方法。
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);
}];
但服务器期待Json格式是
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
)
)
)
)
但我的代码发送的格式错误
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
)
)
)
)
但我检查了NSMutableDictionary
。它是正确的。因为我手动创建了NSJson
字符串。它将提供正确的格式。我的东西afnetworking创建错误的json格式。
我全力以赴。但我仍然有问题
修改
服务器期待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
}
}
]
}
答案 0 :(得分:1)
看起来你必须在另一个字典中扭曲contact_group
字典。尝试这样的事情。
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包装在另一个名为dictArry
的NSMutableDictionary中,以使JSON对象结构成为此。
[0] => Array
(
[contact_group] => Array
(
[guid] => y37845y8y
[name] => Family
[description] => Family members
[isDeleted] => 0
)
)
答案 1 :(得分:0)
您似乎错过了部分中的额外图层:
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"];
你能看出我的意思吗?
忘了还提到,从你假设的预期输出中,似乎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]];
我不确定您上面提供的上述“预期输出”是否准确,您似乎已手动输入。
我已经编写了一个快速演示应用来测试上面的逻辑:
- (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";
}
);
}
)
);
}
哪个符合您的预期输出,所以我认为这里的翻译会丢失。
您能否向我们提供您应用中控制台输出的实际结果,而不是您手动输入的版本?