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

谁都会走 提交于 2019-12-25 07:27:12

问题


I'm trying to send an array in post JSONModel call. I need convett my array to NSString and send the array in format:

[1, 2, 3] 

but when I convert this to NSString and print my array, this has the format:

(1, 2, 3)


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

How can I create this with [] format?


回答1:


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



回答2:


What you can do is

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

Hope this will fix your problem



来源:https://stackoverflow.com/questions/39539360/how-to-get-an-array-type-1-2-instead-of-1-2-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!