Locations

Getting Started with JSON in iOS

Author
Nathan Jones

JSON has taken the data-interchange world by storm with its' lightweight, easy to understand format. The explosion of mobile apps, and their consumption of network based data, helped fuel JSON's growth. CapTech's recommended approach for web services utilizes JSON as the interchange format.

While JSON has been around for several years - RFC4627 was published in July of 2006 - working with JSON in an iOS project required that you download one of the many frameworks and integrate it into your project. My personal choice has been SBJSON - https://github.com/stig/json-framework.

However, with the release of iOS5, Apple has finally included native support for reading and writing JSON with the class NSJSONSerialization.

Why might you consider using the native JSON API? First and foremost, Apple delivered, Apple supported. While support for most third-party libraries is good, you can't beat updated, built-in, backwards compatible support (for future versions of the OS) on day one of a new SDK.

One unfortunate con, use of NSJSONSerialization is limited to devices running iOS5. While iOS5 adoption is moving at a great pace, this still vastly limits your install base. In the case of some enterprises with field devices deployed with previous versions of the OS, this may not be an option for quite some time.

It's important to note, calling any of the NSJSONSerialization methods on a device running an older version of iOS will not cause the application to crash. It simply won't work and will continue on as if the call was never made. Obviously, there are other impacts of this, but this may fit some needs.

Now, let's cover implementing the 4 read/write methods that Apple has exposed. As you can see below, using NSJSONSerialization in your projects is incredibly easy, typically requiring just a few lines of code. NSJSONSerialization supports the reading and writing of JSON data via static data or an instance of NS*Stream. The NS*Stream examples below are rudimentary, but they convey what is needed in order for NSJSONSerialization to do its' job.

Parsing JSON Data

The parsing methods accept an options parameter which allow you to pass different instructions to manipulate what type of foundation object you get back.

NSJSONReadingAllowFragments: instructs the parser to allow top-level objects that are neither an NSArray nor NSDictionary

NSJSONReadingMutableContainers: instructs the parser to generate NSMutableArray and NSMutableDictionary objects

Method: JSONObjectWithData

This method allows us to create a JSON object from an instance of NSData. In this example, we'll assume that an authentication request was successful (see below). The first portion of the code will be to generate the mock-JSON response we received from the service. The code will parse that response and create a foundation object. From there, we'll check if the object is an NSDictionary (there are a couple ways of doing this) and, if so, log the returned message and my token.

Method: JSONObjectWithStream

This method allows us to create a JSON object from an input stream. When and why to use NSInputStream is out of scope for this post. However, if you have a large chunk of JSON data you want to parse, it may be one option to explore.

In this example, we create an instance of NSData that contains JSON formatted tweet information. While not the recommended network communication approach, this allows us to quickly generate an NSData object for our stream. Once the stream is up, we'll create a foundation object from the JSON data, test that it is an NSDictionary (which we expect given the source data we're retrieving), and then log the tweet to the console.

It's important to note that an NS*Stream must be created AND configured in order for NSJSONSerialization to work properly. Once parsed, we simply log each of the tweets to the console.

Method: writeJSONObject:toStream

This method allows us to write our JSON directly to an output steam. Again, when to use NSOutputStream is out of scope for this post, but it is an option. For this example, we'll build an array of all tweet details for my last 3 tweets. We'll then scrub that data down to 3 key pieces of information for each tweet, which we'll write to an output stream. In this case, we'll simply write the contents to a local file.

One additional method I find particularly interesting, isValidJSONObject. An easy way to validate that the object you've created can be converted to JSON data. I could be wrong, but I don't recall any of the third-party libraries I've used having such a method.