JSON and Xcode (iOS): The Basics

For a Swift version of the code see here.

Note: This post should be read as a supplement to the iOS 5 Programming Cookbook example on pages 464-470.

In Xcode we can load JSON from three sources: (1) a file stored within the Xcode project (2) from the Internet (3) we can create our own using NSDictionary and NSArray and NSString.

(1) Let's start with the assumption that we've saved a file called example.json in our project and that it is valid JSON. This is what we'd do to create a path to it:


NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"json"];
// Retrieve local JSON file called example.json


Now we have the path we need to create a pointer to an NSData object. If we want to do this from a file we use dataWithContentsOfFile: like this:

NSError *error = nil; // This so that we can access the error if something goes wrong
NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
// Load the file into an NSData object called JSONData

(2) Often though we'll want a path that takes us to a location on the Internet. So we'd do this instead:

NSURL *internetPath = [NSURL URLWithString:@"http://data.dev8d.org/2012/programme/?event=CS41&output=json"];
// Retrieve JSON from a web address


And if we want our NSData object to derive its data from a web address then we need to use dataWithContentsOfURL: like this:

NSError *error = nil; // This so that we can access the error if something goes wrong
NSData *JSONData = [NSData dataWithContentsOfURL:internetPath options:NSDataReadingMappedIfSafe error:&error];
// This loads JSON from the web into an NSData object called JSONData


(3) In order to create your own JSON in Xcode then you'll need to first create an NSDictionary or NSArray, after which use the NSJSONSerialization class message dataWithJSONObject: to convert this to JSON.


NSError *error = nil; // This so that we can access the error if something goes wrong
NSData *JSONData = [NSJSONSerialization
dataWithJSONObject:dictionary
// Converts data - i.e. NSString, NSArray, NSDictionary - into JSON
options:NSJSONWritingPrettyPrinted
error:&error];

We convert the JSON no matter how it was sourced or created (1, 2 or 3) into something that Xcode can work with by transforming it into a class object with the class method from NSJSONSerialization called JSONObjectWithData: in the following way:


id JSONObject = [NSJSONSerialization
JSONObjectWithData:JSONData
// Creates an Objective-C NSData object from JSON Data
options:NSJSONReadingAllowFragments
error:&error];


(The fact that dataWithJSONObject: converts to JSON and JSONObjectWithData: converts from JSON isn't as transparent as it could be. And it is tempting to use #define in the appName-Prefix.pch file so we have something easier to interpret.)

After we have our JSON object transformed into something we can work with in Xcode we can then use introspection to find out what we have and then extract the contents as we normally would from an NSDictionary or NSArray.

I'm not going to work through the introspection and error handling in code, since as I stated at the beginning of this post, I've written it as a supplement to an iOS 5 Programming Cookbook example (pp. 464-470) and feel that to do so would be unfair on Vandad Nahavandipoor, the author of that book.


Comments