How to convert NSArray of NSStrings into Json String iOS
There is no need to use the following code snippets:
[rollArray componentsJoinedByString:@","]
NSString *str = [Self convertToJSONString:postDict];
You can create JSON by using the following code:
NSArray *rollArray = [NSArray arrayWithObjects:@"1", @"22", @"24", @"11", nil];NSMutableDictionary *postDict = [[NSMutableDictionary alloc]init];[postDict setValue:rollArray forKey:@"existingRoll"];NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];// Checking the formatNSLog(@"%@",[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
Try this :
NSDictionary *object = @{ @"existingRoll":@[@"22",@"34",@"45",@"56"], @"deletedRoll":@[@"20",@"34",@"44",@"56"]};if ([NSJSONSerialization isValidJSONObject:object]) { NSData* data = [ NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:nil ]; NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(str);}
NSMutableDictionary *postDict = [[NSMutableDictionary alloc] init]; [postDict setValue:@"Login" forKey:@"methodName"]; [postDict setValue:@"admin" forKey:@"username"]; [postDict setValue:@"12345" forKey:@"password"]; [postDict setValue:@"mobile" forKey:@"clientType"]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil]; // Checking the format NSString *urlString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; // Convert your data and set your request's HTTPBody property NSString *stringData = [[NSString alloc] initWithFormat:@"jsonRequest=%@", urlString]; //@"jsonRequest={\"methodName\":\"Login\",\"username\":\"admin\",\"password\":\"12345\",\"clientType\":\"web\"}"; NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];