How to convert JSON serialized data to NSDictionary
Please Try the following Code.
NSError* error;NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];NSArray* latestLoans = [json objectForKey:@"loans"];NSLog(@"loans: %@", latestLoans);
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)
Check your json input, could it be that your root element is neither a dictionary nor an array? The documentation says you have to specify NSJSONReadingAllowFragments as the option in this case.
#import <Foundation/Foundation.h>int main(int argc, char *argv[]) { NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init]; NSData *jsonData = [@"{ \"key1\": \"value1\" }" dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *jsonObject=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"jsonObject is %@",jsonObject); [p release];}
Output:
2012-09-26 16:06:51.610 Untitled[19164:707] jsonObject is { key1 = value1;}