NSDictionary 与 NSArray 正在转换为元素数组,同时使用 AfNetworking 2 将数据发布到 PHP
Posted
技术标签:
【中文标题】NSDictionary 与 NSArray 正在转换为元素数组,同时使用 AfNetworking 2 将数据发布到 PHP【英文标题】:NSDictionary with NSArray is converting to array of elements while posting the data to PHP using AfNetworking 2 【发布时间】:2014-02-09 08:59:00 【问题描述】:NSDictionary *partyOne = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], @"type",
[NSNumber numberWithInt:1], @"status",
@"name", @"name",
@"name", @"nid",
@"name", @"nationality",
@"", @"address",
@"name", @"mobile",
nil];
NSDictionary *partyTwo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], @"type",
[NSNumber numberWithInt:1], @"status",
@"name", @"name",
@"name", @"nid",
@"name", @"nationality",
@"", @"address",
@"name", @"mobile",
nil];
NSArray *partyArray = [NSArray arrayWithObjects: partyOne, partyTwo, nil];
NSDictionary *finalData =[NSDictionary dictionaryWithObject: partyArray forKey:@"details"];
当我将上述 NSDictionary 发送到 php 中的 AFNetworking 2 时,如下所示,
// Send the data to server
NSDictionary *regDic = [self generteJasonData];
// Send the registered data to server
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:userName password:userpassWd];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager setShouldUseCredentialStorage:NO];
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:@"http://testlink/submit"parameters:regDic];
[request setHTTPShouldHandleCookies:NO];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];
[operation setResponseSerializer:serializer ];
[operation setShouldUseCredentialStorage:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"Success: %@", responseObject);
[manager.requestSerializer clearAuthorizationHeader];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Failure: %@ responce :%@", error,operation.responseString);
[manager.requestSerializer clearAuthorizationHeader];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"upload", @"") message:NSLocalizedString(@"Some error occureed while uploading data, please tryagain later", @"")
delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[operation start];
我在我的 php 服务器上接收它,
details = (
address = "";
,
mobile = name;
,
name = name;
,
nationality = name;
,
nid = name;
,
status = 1;
,
type = 2;
,
address = "";
,
mobile = name;
,
name = name;
,
nationality = name;
,
nid = name;
,
status = 1;
,
type = 2;
,
);
即我发送了 2 个数组元素,但我收到的数组元素也一样多。 谁能让我知道这里有什么问题。他们是 AFNetworking 2.0 API 的问题还是我的代码有问题。
【问题讨论】:
这里NSDictionary *finalData =[NSDictionary dictionaryWithObject:arrayDictionary forKey:@"details"];
arrayDictionary
是什么?
@katre,我已经编辑了我的查询。它是“partyArray”
我遇到了同样的问题,Vaz,你找到解决方案了吗?
【参考方案1】:
好的,所以你需要做的就是修复方法
NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value)
在文件 AFURLRequestSerialization.m 中: 而不是
for (id nestedValue in array)
做起来
for (int i = 0;i < [array count];++i)
然后代替
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];
做起来
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[%d]", key,i], nestedValue)];
【讨论】:
精湛的答案兄弟。我很震惊为什么这个答案不被接受,但你应该解释一下 老兄,你救了我的命 :),我只花了半天时间来解决这个问题。非常感谢【参考方案2】:AFNetworking 2.0:我使用了@gilad 答案并对其进行了一些修改以使其正常工作。所以我所做的写在下面:
NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value)
NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector(compare:)];
if ([value isKindOfClass:[NSDictionary class]])
NSDictionary *dictionary = value;
// Sort dictionary keys to ensure consistent ordering in query string, which is important when deserializing potentially ambiguous sequences, such as an array of dictionaries
for (id nestedKey in [dictionary.allKeys sortedArrayUsingDescriptors:@[ sortDescriptor ]])
id nestedValue = [dictionary objectForKey:nestedKey];
if (nestedValue)
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue((key ? [NSString stringWithFormat:@"%@[%@]", key, nestedKey] : nestedKey), nestedValue)];
else if ([value isKindOfClass:[NSArray class]])
// This is modified by me
NSArray *array = value;
int i = 0;
for (id nestedValue in array)
// A check has been added for NSDictionary
if ([nestedValue isKindOfClass:[NSDictionary class]])
//Here i used @gilad's answer
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[%d]", key,i], nestedValue)];
else
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];
i++;
else if ([value isKindOfClass:[NSSet class]])
NSSet *set = value;
for (id obj in [set sortedArrayUsingDescriptors:@[ sortDescriptor ]])
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue(key, obj)];
else
[mutableQueryStringComponents addObject:[[AFQueryStringPair alloc] initWithField:key value:value]];
return mutableQueryStringComponents;
如上所述,我刚刚在 AFURLRequestSerialization.m 类中编辑了 AFQueryStringPairsFromKeyAndValue,然后它开始按我们想要的方式工作。
【讨论】:
这个怎么用?我需要使用以下格式请求, [ "path": "/", "value": "state": "ACTIVE" , "op": "replace" ] 你能解释一下吗?告诉我你的使用代码.. @karthikeyan 我的请求类似于 [, ,] 格式。正如我在回答中提到的那样,我已经用地雷替换了 NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value) 这个方法 @karthikeyan 如果您仍在寻找解决方案,那么您可以告诉我。我可以尝试给你编辑过的 AFNetworking 框架 感谢重播,我已经做到了,通过使用 NSMutableString,我只是像这样制作了格式,并转换为数据,然后将其发送到服务器,这对我有用,以上是关于NSDictionary 与 NSArray 正在转换为元素数组,同时使用 AfNetworking 2 将数据发布到 PHP的主要内容,如果未能解决你的问题,请参考以下文章
如何将变量与 NSArray 中的 NSDictionary 中的对象进行比较?
nsdictionary 上的排序给出的结果与 nsarray 上的排序不同
带有 NSArray 的 UITableview 包含 NSDictionary 和 NSDictionary 包含另一个 NSDictionary
使用 NSCoder 编码 NSArray 或 NSDictionary