0

I need send to my request server an array in parameters, but this can't be contained between "".

I need send to my server the key users with the array [2, 5, 6] value like this:

"users" : [2, 5, 6]

I'm trying do this but I only can get the array in format ( 2, 5, 6) or like this:

 "[2, 5, 6]"

This code only get the array with "":

NSMutableArray *array= [NSMutableArray arrayWithObjects:@"1", @"2",@"3",@"4", nil];
NSData *jsond = [NSJSONSerialization dataWithJSONObject: array options:NSJSONWritingPrettyPrinted error:NULL];
NSString *json = [[NSString alloc] initWithData:jsond encoding:NSUTF8StringEncoding];

How can I send my array in parameters in the correct format without ""?

user3745888
  • 6,143
  • 15
  • 48
  • 97
  • Update your question with relevant code showing what you are printing and how is was created, and your printed it (to see the quotes). – rmaddy Sep 16 '16 at 22:20
  • I'm updated my question with my code. Now I can show my array with "", but I can send this in parameters post json without "" – user3745888 Sep 16 '16 at 22:39
  • Does the value of `json` have the unwanted quotes? – rmaddy Sep 16 '16 at 22:41
  • That code doesn't output what we're seeing. Where is "users" coming from? Can you send the actual code please? – TyR Sep 16 '16 at 22:41
  • I had explained bad, "users" is the key of the dictionary that I want send to my server, but this questions is for convert the array to format [1,2,3] and add this array to the key "users" – user3745888 Sep 16 '16 at 22:48
  • Take the string, "[1,2,3]", and run it through NSJSONSerialization . Then you will have an NSArray. Add that to your NSMutableDictionary, under the key "users". I still don't quite know what you're doing but it sounds like maybe you're inserting JSON strings into things that should be holding arrays. The full code would help. – TyR Sep 16 '16 at 22:50
  • down voted because the question is unclear, the sample code doesn't generate the output in the question, and also this Q is very similar to OP's other Q asked a few hours ago: http://stackoverflow.com/questions/39539360/how-to-get-an-array-type-1-2-instead-of-1-2-in-objective-c – TyR Sep 16 '16 at 23:01

1 Answers1

1

To get the output you need in question text try the following

NSDictionary *dict= @{@"users" : @[@1, @2, @3, @4]};
NSData *jsond = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
NSString *json = [[NSString alloc] initWithData:jsond encoding:NSUTF8StringEncoding];

The output is {"users":[1,2,3,4]}

malex
  • 9,874
  • 3
  • 56
  • 77