当 key 不是字符串时,iOS HTTP Post 方法不起作用

标签 ios objective-c http-post nsurlconnection

我正在使用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/

相关文章:

ios - 使用 NSPredicate 搜索 - 全名

ios - 实现 Spring 库跟踪(移动应用传感器)

未执行 Swift dataTaskWithRequest 完成 block

HTTP 400 - 描述最少的错误代码难以理解

android - 改造 @Body 在 HTTP 请求中显示为参数

ios - 欢迎屏幕中的视频使用 AVPlayer 导致内存泄漏?

ios - Safari Web 检查器在调试 iPhone 时不显示元素和样式面板

ios - Git 冲突导致 Storyboard 问题

ios - 不兼容的指针类型用类型为“BaseClass *”的表达式初始化 'SubClass *__strong'

objective-c - 在 Objective-C 上 : which is the root directory in iphone