如何在 Objective-c 中获取数组类型 [1,2] 而不是 (1, 2)?

How to get an array type [1,2] instead of (1, 2) in Objective-c?

我正在尝试在 post JSONModel 调用中发送一个数组。我需要将我的数组转换为 NSString 并以以下格式发送数组:

[1, 2, 3] 

但是当我将其转换为 NSString 并打印我的数组时,其格式为:

(1, 2, 3)


NSMutableArray *array= [NSMutableArray arrayWithObjects:@"1", @"2",@"3",@"4", nil];
NSString *arraString = [NSString stringWithFormat:@"%@", arr];
NSLog(@"%@",arraString);

如何用 [] 格式创建这个?

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];

NSLog(@"%@", json);

你能做的是

NSString *joinedString = [array componentsJoinedByString:@","];
NSString *arraString = [NSString stringWithFormat:@"(%@)", joinedString];

希望这能解决您的问题