Karuppu MGR Karuppu MGR - 1 year ago 116
Objective-C Question

Converting NSMutableArray to JSONArray

Im trying to create a json array out of nsmutable array.

To do so, Im trying to convert nsmutable array to nsdata as follows,

NSData *dataArray = [NSKeyedArchiver archivedDataWithRootObject:self.countryNameArray];

is a mutable array that contains names of countries.

Then Im trying to create a json array out of NSData as follows

NSString* jsonArray = [[NSString alloc] initWithBytes:[dataArray bytes] length:[dataArray length] encoding:NSUTF8StringEncoding];

but the problem is that,
is null when Im trying to use it.

Answer Source
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];