如何在没有问题的情况下在NSDictionary中发布NSArray NSArray? [英] How can I POST an NSArray of NSDictionaries inside an NSDictionary without problems?
问题描述
我知道怎么做,这很简单。
I do know how to do this, it's fairly simple.
问题是它不起作用。
这是我用来POST数据的函数:
Here's the function I use to POST the data:
- (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字段中保存的内容:
Here's what gets saved in the MySQL field:
[{URL = "http://www.google.com";},{title = Google;},{URL = "http://www.yahoo.com";},{title = Yahoo;}]
这太疯狂了!
- 我已经在MySQL字段内的字典中成功保存了多个属性的字典数组的JSON - 或者我想在这里做什么 - 使用PHP脚本用于不同目的并且它可以正常工作,没问题。
- 我使用相同的PHP代码来保存它到MySQL字段,所以它不是PHP的故障。
- 我使用AFNetworking完成的所有其他保存/检索功能。
- I have successfully saved JSON of an array of dictionaries with multiple attributes inside a dictionary inside a MySQL field --or in short what I'm trying to do here-- using a PHP script for a different purpose and it works, no problem.
- I use the same PHP code to save it to the MySQL field so IT'S NOT PHP'S FAULT.
- All other save / retrieve functions I have made using AFNetworking work perfectly.
这有效:
@[@{@"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;
}
);
}
INSANE!
出于某种原因,如果我添加额外的属性,它会崩溃。
For some reason, it breaks down if I add an extra attribute.
这必须是 AFNetworking 错误因为没有意义。
This must be an AFNetworking bug because it makes no sense.
编辑:
我可以:
-
制作两个MySQL字段:websiteTitles,websiteURLs。
Make two MySQL fields: websiteTitles, websiteURLs.
将其保存为一个字符串:谷歌; http://www.google.com 然后将其分开,但这违背了使用JSON的目的。
Save it as one string: "Google;http://www.google.com" and then separate it but that defeats the purpose of using JSON.
将参数切成两半:websteTitles,websiteURLs
Send the parameters chopped in half: websteTitles, websiteURLs
一切都很可怕,有什么想法吗?
All are hideous, any ideas?
编辑2:
我运行了一些测试:
如果数组有1个或2个项目,它的行为仍然无关紧要。
It doesn't matter if the array has 1 or 2 items it still behaves like this.
我尝试了 rob180 的建议,并且 - 如预期的那样 - 这是AFNetwokring的错:
I tried what rob180 suggested and --as expected-- it's AFNetwokring's fault:
{
websites = (
{
URL = "http://www.google.com";
},
{
title = Google;
},
{
URL = "http://www.yahoo.com";
},
{
title = Yahoo;
}
);
}
这是从应用程序发送的实际服务器响应,没有中间的mysql。
This is the actual server response of what has been send from the app, no mysql in the middle.
编辑3:
REQUEST: <NSMutableURLRequest: 0x7f9352d467e0> { URL: http://notreal/updateWebsites.php }
HTTPBody 看起来像这样:
<63726564 656e7469 616c735b ... 653d30>
如何解码?
另外,我正在使用 AFHTTPRequestSerializer 。也许,如果我将其更改为 AFJSONRequestSerializer ,它将解决问题,但我真的不想,因为我已经用这种方式编写了很多方法。
Also, I'm using an AFHTTPRequestSerializer. Maybe, if I change it to AFJSONRequestSerializer it will fix the problem but I really don't want to since I have written many methods this way.
推荐答案
查询字符串参数化根本不是编码嵌套数据结构的可靠方法。这就是为什么所有现代Web框架都有内置的便利,可以自动将传入的JSON解码为参数。 - Mattt Thompson
所以, JSON 它是...
So, JSON it is...
参数:
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";
}
全部完成!
纯金。
这篇关于如何在没有问题的情况下在NSDictionary中发布NSArray NSArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!