Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.


1 Answers

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
like image 61
Bhadresh Mulsaniya Avatar answered Dec 24 '25 09:12

Bhadresh Mulsaniya