我正在使用AFNetworking使用HTTP POST将一些JSON数据发送到服务器。 AFNetworking使用setParameterEncoding:AFJSONParameterEncoding
配置。
所以我构建了我的请求参数:
NSMutableDictionary* params = [NSMutableDictionary dictionary];
NSMutableArray* users = [NSMutableArray array];
for (NSDictionary *localEntity in SOMECOREDATAENTITIES)
{
// create a new dict. "user"
NSMutableDictionary* user = [NSMutableDictionary dictionary];
// add basic parameters for our user
[user setValue:[localEntity valueForKey:@"firstname"] forKey:@"first_name"];
[user setValue:[localEntity valueForKey:@"lastname"] forKey:@"last_name"];
//one level down: create a new dict. and give it some key-value pairs
NSMutableDictionary* device = [NSMutableDictionary dictionary];
[device setValue:localEntity[@"imei"] forKey:@"device_imei"];
// add the new dict. to the user
[user setValue:device forKey:@"device"];
//two levels down: create a new array and add multiple dict. to it
NSMutableDictionary* oEntitys = localEntity[@"orders"];
NSMutableArray* orders = [NSMutableArray array];
for (NSDictionary *orderEntity in oEntitys)
{
NSMutableDictionary* order = [NSMutableDictionary dictionary];
[order setValue:[orderEntity valueForKey:@"orderID"] forKey:@"order_id"];
[orders addObject:order];
}
// add the array containing multiple NSDictionarys to the user dict.
[user setValue:orders forKey:@"orders"];
// finally add our user to the users dict.
[users addObject:user];
}
// we add our users dict to the AFNetworking parameters
[params setValue:users forKey:@"users"];
// we add some more settings to the AFNetworking parameters
[params setValue:@"SOMEVALUE" forKey:@"SOMEKEY"];
如你所见,我们在这里得到了几乎所有东西:带有一些键值对的NSDictionary,还有嵌套的NSDictionary对象和嵌套在NSDictrionaries中的NSArrays。 在查看网络流量发送时,我看到发送的JSON对象如下所示:
注意:数据发送是一个关键:具有值的用户:“对象”。我无法查看那个“对象”,因为实际上它不是对象,只是一个名为“object”的字符串!这就是问题所在。
然而,AFNetworking设置似乎很好(我能够深入挖掘“用户” - 对象) - 这只是NSDictionary的问题 - 我想 - 如果我使用这样的东西进行测试:
params = @{@"users":
@{
firstname: @"SOMENAMEVALUE",
lastname: @"SOMENAMEVALUE"
},
@"SOMEKEY": @"SOMEVALUE"]
};
然后我嗅到的流量看起来是正确的:
我在这里缺少什么?