IOSDev
IOSDev

Reputation: 680

How to post an array value in JSON

I want to post an array value in JSON.

Below is my code :

-(void)getConnection {

    NSArray *comment=[NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",@"hello,yes,tell", nil];

    NSURL *aurl=[NSURL URLWithString:@"http://sajalaya.com/taskblazer/staffend/form/iphonearraytest.php"];

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:aurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [request setHTTPMethod:@"POST"];

   NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:comment options:NSJSONWritingPrettyPrinted error:nil];
   NSString *new = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];

 // NSString *new = [comment JSONString];

 // NSArray *new=[comment jsonvalue];

    NSString *postString=[NSString stringWithFormat:@"tag=&comment=%@&total=%@",new,@"4"];

    NSLog(@"this is post string%@",postString);
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

     [NSURLConnection connectionWithRequest:request delegate:self];       
}

Upvotes: 4

Views: 1006

Answers (2)

Michal Zaborowski
Michal Zaborowski

Reputation: 5099

We don't know your question, but my answer is short and simple. You should use great open source library for this, which is: AFNetworking, and do request like this:

_httpClient = [[AFHTTPClient alloc] initWithBaseURL:[[NSURL alloc] initWithString:@"http://sajalaya.com"]];

[_httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:comment options:NSJSONWritingPrettyPrinted error:nil];
NSString *new = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        new, @"comment",
                        @4, @"total,
                        nil];

NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"POST"
                                                             path:@"/taskblazer/staffend/form/iphonearraytest.php"
                                                       parameters:params];
request.timeoutInterval = 8;

AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
      // success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      // failure
}];

Upvotes: 1

iDhaval
iDhaval

Reputation: 7844

Please use the following Mutable Array Operation componentsJoinedByString.

e.g.

NSMutableArray *commennts=[NSMutableArray arrayWithObjects:@"aaa",@"bbb",@"ccc",@"hello,yes,tell", nil];

NSString* strCommentsJoin = [commennts componentsJoinedByString:@","]; // Please use your separator 

Upvotes: 1

Related Questions