Reputation: 1802
I am using Post method to send user data to server.
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];
}
}];
Here I have some keys like "user[first_name] , user[last_name] ..etc". If it is only "first_name" then I can pass this key as string. but "first_name" is under "user" so I have passed like "user[first_name]".
This code is not working and not update data into server. I want to know the correct way to pass the data if key is like this ("user[first_name]").
Note : "first_name" is under "user"
Can anyone please help me to solve this?
Thanks in advance.
Upvotes: 0
Views: 134
Reputation: 26066
Your issue is here:
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];
I'll skip the fact that two var begins with a uppercase.
Well, if you read a little about JSON, or you're used, in Objective-C, to use short hand syntax (it exists in other languages too), you can assume that in fact, your web service is awaiting for something like that:
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"}}};
These are the results of the userRegistrationDictionary
if you try then to convert it into 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"
}
}
Yours seems "weird". It's valid, but unusual. Everything is in the same level, but ZzZ[key] seems to look for a dictionary thing.
Upvotes: 1