I am new to json, I want to pass the NSArray inside the NSDictionary as a parameter along with the url to server, how it can be done while method is post ? I had tried sample codes but not found my exact solution.below is my sample code
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"contact_name", @"kumar",@"designantion",@"sales", nil];
NSDictionary *finalDict = [[NSDictionary alloc] initWithObjectsAndKeys:dict, @"OfficeContactPerson", nil];
NSString *post =[[NSString alloc]initWithFormat:@"name=%@&short_name=%@&shop_no=%@&door_no=%@&floor=%@&building=%@&street=%@&area=%@&main=%@&city=%@&state=%@&district=%@&pincode=%@&telephone=%@&contact_name=%@&designatio=%@&mobile=%@&email=%@&OfficeContactPerson=%@",self.txtshowroom.text,self.txtshortname.text,self.txtshop.text,self.txtdoor.text,self.txtfloor.text,self.txtbuilding.text,self.txtstreet.text,self.txtarea.text,self.txtmain.text,cityineger,stateintegervalue,districtintegervalue,self.txtpincode.text,self.txttel.text,self.txtname.text,self.txtdesign.text,self.txtmbl.text,self.txtemail.text,finalDict];
NSLog(@"postdata :%@",post);
NSURL *url=[NSURL URLWithString:@"http://139.59.252.34:1337/office"];
NSData *postData=[post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postlength=[NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request= [[NSMutableURLRequest alloc]init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postlength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error=[[NSError alloc]init];
NSHTTPURLResponse *response=nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!urlData){
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Error"message:FS_ERROR_LOCALIZED_DESCRIPTION(error.code)
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
NSString *responseData=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response data ----->%@",responseData);
NSError *error=nil;
NSDictionary *jsonData=[NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingMutableContainers error:&error];
);
}
OfficeContactPerson": [
{
"office": 29,
"contact_name": "Priya",
"designation": "Prop",
"department": "ALL",
"mobile": "1231231231",
"email": "[email protected]",
"incharge_status": null,
"created_by": null,
"modified_by": null,
"id": 14,
"createdAt": "2016-04-19T13:43:59.000Z",
"updatedAt": "2016-04-19T14:25:36.000Z"
}
]