gpt4 book ai didi

ios - 具有嵌套 NSDictionary 的 AFNetworking POST 方法

转载 作者:可可西里 更新时间:2023-11-01 06:07:22 25 4
gpt4 key购买 nike

我正在使用 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

}

}

]

}

最佳答案

看起来您必须将 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
)

)

关于ios - 具有嵌套 NSDictionary 的 AFNetworking POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24865943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com