我正在使用Post方法将用户数据发送到服务器。
NSMutableArray *RegistraionKeys=[NSMutableArray arrayWithObjects:@"user[first_name]",@"user[last_name]",@"user[user_profile_attributes][date_of_birth]",@"user[user_profile_attributes][address]",@"user[user_profile_attributes][country]", @"user[user_profile_attributes][phone]", @"user[user_profile_attributes][gender]", nil];
NSMutableArray *RegistraionValues=[NSMutableArray arrayWithObjects:@"aaa", @"GS", @"1987-07-19", @"bbb",@"IND", @"1234567890", @"male",nil];
NSDictionary *userRegistrationDictionary = [[NSDictionary alloc]initWithObjects:RegistraionValues forKeys:RegistraionKeys];
NSData *postData = [NSJSONSerialization dataWithJSONObject:userRegistrationDictionary options:0 error:&error];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:method];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"en" forHTTPHeaderField:@"LOCALE"];
[request setHTTPBody:body];
NSURLSessionDataTask *uploadTask = [sessionManager dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *results;
if (data) {
results =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
}
}];
这里我有一些键,例如“user[first_name],user[last_name] ..etc”。如果它只是“first_name”,那么我可以将此键作为字符串传递。但“first_name”位于“user”下,所以我像“user[first_name]”一样通过了。
此代码不起作用,并且不会将数据更新到服务器中。如果 key 是这样的(“user[first_name]”),我想知道传递数据的正确方法。
注意:“first_name”位于“user”下
谁能帮我解决这个问题吗?
提前致谢。
最佳答案
您的问题在这里:
NSMutableArray *RegistraionKeys=[NSMutableArray arrayWithObjects:@"user[first_name]",@"user[last_name]",@"user[user_profile_attributes][date_of_birth]",@"user[user_profile_attributes][address]",@"user[user_profile_attributes][country]", @"user[user_profile_attributes][phone]", @"user[user_profile_attributes][gender]", nil];
NSMutableArray *RegistraionValues=[NSMutableArray arrayWithObjects:@"aaa", @"GS", @"1987-07-19", @"bbb",@"IND", @"1234567890", @"male",nil];
NSDictionary *userRegistrationDictionary = [[NSDictionary alloc]initWithObjects:RegistraionValues forKeys:RegistraionKeys];
我将跳过两个 var 以大写字母开头的事实。
好吧,如果您阅读了一些关于 JSON 的内容,或者您习惯于在 Objective-C 中使用速记语法(其他语言中也存在),您可以假设实际上您的 Web 服务正在等待对于这样的事情:
NSDictionary *userRegistrationDictionary = @{@"user":@{@"first_name":@"aaa",
@"last_name":@"GS",
@"user_profile_attributes":@{@"date_of_birth":@"1987-07-19",
@"address":@"bbb",
@"country":@"IND",
@"phone":@"1234567890",
@"gender":@"male"}}};
如果您尝试将其转换为 JSON,这些是 userRegistrationDictionary
的结果:
$>YourFirstVersion: {
"user[user_profile_attributes][country]" : "IND",
"user[user_profile_attributes][gender]" : "male",
"user[first_name]" : "aaa",
"user[last_name]" : "GS",
"user[user_profile_attributes][date_of_birth]" : "1987-07-19",
"user[user_profile_attributes][phone]" : "1234567890",
"user[user_profile_attributes][address]" : "bbb"
}
$>CorrectVersion: {
"user" : {
"user_profile_attributes" : {
"gender" : "male",
"phone" : "1234567890",
"country" : "IND",
"date_of_birth" : "1987-07-19",
"address" : "bbb"
},
"first_name" : "aaa",
"last_name" : "GS"
}
}
你的看起来很“奇怪”。这是有效的,但不寻常。一切都在同一层,但 ZzZ[key] 似乎在寻找字典的东西。
关于当 key 不是字符串时,iOS HTTP Post 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36195119/