iOS HTTP Post 方法在键不是字符串时不起作用

iOS HTTP Post method not working when key is not a string

我正在使用 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]"。

此代码无效,无法将数据更新到服务器中。如果密钥是这样的("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 中习惯使用速记语法(它也存在于其他语言中),你可以假设事实上,您的网络服务正在等待类似的东西:

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"}}};

这些是 userRegistrationDictionary 的结果,如果您尝试将其转换为 JSON:

$>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"
  }
}

你的好像是"weird"。这是有效的,但不寻常。一切都在同一层级,但是ZzZ[key]好像是在找字典的东西。