Mastering iOS Frameworks: Working with and Parsing JSON
JSON is a great way to send data back and forth between servers, Web sites, and iOS apps. It is lighter and easier to handle than XML, and with iOS’s built-in support for JSON, it is easy to integrate into an iOS project. Many popular Web sites, including Flickr, Twitter, and Google, offer APIs that provide results in JSON format, and many languages offer JSON support. This chapter demonstrates how to parse and present JSON from a sample message-board server in an app, and encode a new message entry in JSON to send to the server.
JSON
JavaScript Object Notation (JSON) is a lightweight format for sharing data. It is technically a part of the language JavaScript and provides a way to serialize JavaScript objects; however, practically, it is supported in a wide variety of programming languages, making it a great candidate for sharing data between different platforms. JSON also has the benefit of being human-readable.
JSON has a simple and intuitive syntax. At its most basic level, a JSON document can contain objects, which are essentially key-value dictionaries like what Objective-C programmers are familiar with, or arrays. JSON can contain arrays of objects and arrays of values, and can nest arrays and objects. Values stored in JSON, either in arrays or associated with a key, can be other JSON objects, strings, numbers, or arrays, or true, false, or null.
Benefits of Using JSON
There are many reasons to use JSON in an iOS app:
- Server Support: Communicating information to and from a remote server is a common use case for iOS apps. Since so many server languages have built-in support for JSON, it is a natural choice as a data format.
- Lightweight: JSON has little formatting overhead when compared to XML and can present a significant savings in the amount of bandwidth needed to transmit data between a server and a device.
- iOS Support: JSON is now fully supported as of iOS 5 with the addition of the NSJSONSerialization class. This class can conveniently provide an NSDictionary or NSArray (or even mutable varieties) from JSON data or can encode an NSDictionary or NSArray into JSON.
- Presentation and Native Handling: The simplest method to get data from a server to an iOS device is just to use a UIWebView and display a Web page; however, this approach has drawbacks in terms of performance and presentation. In many cases it is much better to just pull the data from the server, and present it on the device using native tools like UITableView. Performance can be much better, and presentation can be optimized to work on iOS screen sizes and take advantage of available retina displays.
JSON Resources
For more information on JSON, visit http://json.org. That site has a formal definition of JSON, with specific information on format and syntax.
The Sample App
The sample app for this chapter is Message Board, including a Ruby on Rails server and an iOS app.
The Ruby on Rails server consists of just one object: the message. It has been set up to support sending a list of messages in JSON, and to accept new messages in JSON format. The server also supports Web-based interactions.
The iOS app will pull messages from the server and display them in a standard table view and will be able to post new messages to the server in JSON format.
Accessing the Server
To view the Message Board Ruby on Rails server, visit http://freezing-cloud-6077.herokuapp.com/. The Messages home screen will be visible, as shown in Figure 9.1.
Figure 9.1 Messages home screen.
The messages server has been set up to handle creating and displaying messages on the Web and with JSON.