Karuppu MGR Karuppu MGR - 6 months ago 38
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];


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,
jsonArray
is null when Im trying to use it.

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