我怎样才能在NSDictionary内张贴一个NSDray的NSArray没有问题?

我知道如何做到这一点,这很简单。

问题是它不起作用。

这是我用来POST数据的function:

- (void)updateWebsitesUsingParameters:(NSDictionary *)parameters; { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:@"http://notreal/updateWebsites.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); //... } failure:^(AFHTTPRequestOperation *operation, NSError *error) { //... }]; } 

这里是参数:

 NSDictionary *parameters = @{@"type" : @"0", @"credentials" : @{@"email" : @"notreal@gmail.com", @"password" : @"notreal"}, @"device" : @{@"ID" : @"8588107756600540", @"numberOfSessions" : @"0", @"name" : @"Nick's iMac"}, @"websites" : @[@{@"title" : @"Google", @"URL" : @"http://www.google.com"}, @{@"title" : @"Yahoo", @"URL" : @"http://www.yahoo.com"}]}; 

以下是MySQL字段中保存的内容:

 [{URL = "http://www.google.com";},{title = Google;},{URL = "http://www.yahoo.com";},{title = Yahoo;}] 

这太疯狂了!

  1. 我已经成功地保存了字段数组中的字典数组的JSON,在MySQL字段内​​的字典中 – 或简而言之,我正在尝试在这里 – 使用PHP脚本用于不同的目的,它的工作,没有问题。
  2. 我使用相同的PHP代码将其保存到MySQL字段,所以它不是PHP的错误。
  3. 所有其他的保存/检索function,我使用AFNetworking完美的工作。

这工作:

 @[@{@"title" : @"Google"}, @{@"title" : @"Yahoo"}] 

这不:

 @[@{@"title" : @"Google", @"URL" : @"http://www.google.com"}, @{@"title" : @"Yahoo", @"URL" : @"http://www.yahoo.com"}] 

这是回应:

 { websites = ( { URL = "http://www.google.com"; }, { title = Google; }, { URL = "http://www.yahoo.com"; }, { title = Yahoo; } ); } 

疯!

出于某种原因,如果我添加一个额外的属性,它会崩溃。

这必须是一个AFNetworking错误,因为它没有任何意义。

编辑:

我可以:

  1. 创build两个MySQL字段:websiteTitles,websiteURLs。

  2. 将其另存为一个string:“Google; http://www.google.com ”,然后将其分开,但是这违背了使用JSON的目的。

  3. 发送参数:websteTitles,websiteURLs

一切都很可怕,有什么想法?

编辑2:

我运行一些testing:

如果数组有1个或2个项目,它的行为如此。

我尝试了rob180的build议和 – 预期 – 这是AFNetwokring的错:

 { websites = ( { URL = "http://www.google.com"; }, { title = Google; }, { URL = "http://www.yahoo.com"; }, { title = Yahoo; } ); } 

这是从应用程序发送的实际服务器响应,中间没有mysql。

编辑3:

 REQUEST: <NSMutableURLRequest: 0x7f9352d467e0> { URL: http://notreal/updateWebsites.php } 

HTTPBody看起来像这样:

 <63726564 656e7469 616c735b ... 653d30> 

我怎样才能解码这个?

另外,我正在使用AFHTTPRequestSerializer 。 也许,如果我将其更改为AFJSONRequestSerializer它将解决这个问题,但我真的不想,因为我已经写了很多这样的方法。

“查询string参数化并不是编码嵌套数据结构的可靠方法,这就是为什么所有现代Web框架都有内置的便利function,可以自动将传入的JSON解码为参数。马特汤普森

所以, JSON是…

参数:

 NSDictionary *parameters = @{@"websites" : @[@{@"Title" : @"Google", @"URL" : @"http://www.google.com"}, @{@"Title" : @"Yahoo", @"URL" : @"http://www.yahoo.com"}]}; 

发送:

 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; [manager POST:@"http://server/path/file.php" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"Error: %@", error.description); }]; 

检索:

 <?php header('Content-type: application/json'); $request = json_decode(file_get_contents('php://input'), TRUE); $response = ["URLOfTheSecondWebsite" => $request['websites'][1]['URL']]; echo json_encode($response); ?> 

响应:

 { URL = "http://yahoo.com"; } 

全做完了!

纯金。

更新函数AFQueryStringPairsFromKeyAndValue

更改

  for (id nestedValue in array) { [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)]; } 

  int i = 0; for (id nestedValue in array) { [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[%d]", key, i++], nestedValue)]; }