Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert JSON serialized data to NSDictionary

I have used this method,

NSDictionary *jsonObject=[NSJSONSerialization         JSONObjectWithData:jsonData                    options:NSJSONReadingMutableLeaves                      error:nil]; NSLog(@"jsonObject is %@",jsonObject); 

It's printing "jsonObject is null".
Is there any problem with "error:nil".
I am not using any url or connection methods.
I have a json file and I want to display it in a table.

like image 661
Rajesh Avatar asked Sep 26 '12 13:09

Rajesh


2 Answers

Please Try the following Code.

NSError* error; NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData                                                      options:kNilOptions                                                         error:&error];  NSArray* latestLoans = [json objectForKey:@"loans"];  NSLog(@"loans: %@", latestLoans); 
like image 131
amit soni Avatar answered Sep 21 '22 02:09

amit soni


Just in case anyone is here to see swift code:

NSData to NSDictionary

let dictionary:NSDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(jsonData)! as NSDictionary 

NSData to NSString

let resstr = NSString(data: res, encoding: NSUTF8StringEncoding) 
like image 45
iAhmed Avatar answered Sep 19 '22 02:09

iAhmed