Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios post请求的JSON参数中的AFN发送数组_Ios_Arrays_Json_Post_Afnetworking - Fatal编程技术网

Ios post请求的JSON参数中的AFN发送数组

Ios post请求的JSON参数中的AFN发送数组,ios,arrays,json,post,afnetworking,Ios,Arrays,Json,Post,Afnetworking,我试图通过POST将参数发送到我的服务器,它通常可以正常工作,但我不知道如何发送包含数组作为参数之一的JSON。以下是我尝试过的: AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:myURL]]; NSMutableArray *objectsInCart = [NSMutableArray arrayWithCapacity:[_cart count]]; for(NSD

我试图通过POST将参数发送到我的服务器,它通常可以正常工作,但我不知道如何发送包含数组作为参数之一的JSON。以下是我尝试过的:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:myURL]];
NSMutableArray *objectsInCart = [NSMutableArray arrayWithCapacity:[_cart count]];
for(NSDictionary *dict in _cart)
{
    NSObject *object = [dict objectForKey:@"object"];
    NSDictionary *objectDict = @{@"product_id": [NSString stringWithFormat:@"%d",[object productID]],
                                 @"quantity": [NSString stringWithFormat:@"%d", [[dict objectForKey:@"count"] intValue]],
                                 @"store_id": [NSString stringWithFormat:@"%d", [Store getStoreID]],
                                 @"price": [NSString stringWithFormat:@"%.2f", [object price]]};
    [objectsInCart addObject:objectDict];
}
NSError *error = nil;
NSString *cartJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart
                                                                                    options:NSJSONWritingPrettyPrinted
                                                                                      error:&error]
                                           encoding:NSUTF8StringEncoding];

if(error)
{
    NSLog(@"Error serializing cart to JSON: %@", [error description]);
    return;
}

NSDictionary *parameters = @{@"status": @"SUBMITTED",
                             @"orders": cartJSON};

NSMutableURLRequest *orderRequest = [httpClient requestWithMethod:@"POST"
                                                             path:@"/app/carts"
                                                       parameters:parameters];

AFJSONRequestOperation *JSONOperation = [[AFJSONRequestOperation alloc] initWithRequest:orderRequest];

但是,我在发送此JSON时出错。任何建议都将不胜感激

我看不出您在哪里指定要发布JSON,因此我打赌您发送的是表单URL参数编码,根据AFHTTPClient文档,它是这样的:

如果查询字符串对的值有一个
NSArray
,则数组的每个成员将以
字段[]=value1和字段[]=value2的格式表示。否则,该对将被格式化为“field=value”。使用
-description
方法导出键和值的字符串表示形式。构造的查询字符串不包括?用于分隔查询组件的字符

如果您的服务器确实希望您发布JSON,请在第二行添加以下内容,告诉AFNetworking:

// AFNetworking 1.0
// httpClient is a subclass of AFHTTPClient
httpClient.parameterEncoding = AFJSONParameterEncoding;

// AFNetworking 2.0
// httpClient is a subclass of AFHTTPRequestOperationManager or AFHTTPSessionManager
httpClient.requestSerializer = AFJSONRequestSerializer;
然后,您将删除对
NSJSONSerialization
的调用,只需将
objectsInCart
放入
参数
字典中即可


旁注:将
AFHTTPRequestOperationManager
AFHTTPSessionManager
(AFNetworking 2.0)或
AFHTTPClient
(AFNetworking 1.0)子类化并将这种类型的配置放在
initWithBaseURL:
方法中是正常的。(您可能不想为每个请求启动一个新的客户端。)

我不知道服务器需要什么,但通常JSON中的每个项都有一个键,包括数组。您现在只是发送数组,没有密钥。请尝试使用`NSString*cartJSON=@“'products':%@,[[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart选项:NSJSONWriting预打印错误:&错误]编码:NSUTF8StringEncoding];如果您查看
参数
字典,数组的键是
@“orders”
好的,很遗憾我错过了。您是否查看了通过线路发送的实际数据?我非常重视像Charles proxy这样的应用程序,它可以拦截从我的应用程序到外部服务器的所有流量。为了将来参考使用AFNetworking 2.xx的其他应用程序,您不必将任何数组或字典序列化为JSON,只需将它们原样传递给您的AFHTTPRequestOperationManager,AFJSONRequestSerializer将处理所有事情,但不要忘记添加
manager.requestSerializer=[AFJSONRequestSerializer]如接受的答案中所述。是的,我之前发现了这个,应该更新帖子。谢谢