23 Mar 2017

Converting JSON into an Objective-C object



Converting JSON into an Objective-C object

// Retrieve local JSON file called example.json

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"json"];

 // Load the file into an NSData object called JSONData 

NSError *error = nil;

NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];

 // Create an Objective-C object from JSON Data

id JSONObject = [NSJSONSerialization
                         JSONObjectWithData:JSONData
                         options:NSJSONReadingAllowFragments
                         error:&error];

No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0