English | 2015 | ISBN: 978-1-78528-690-2 | 192 Pages | PDF, EPUB, MOBI | 10 MB
JSON on Mobile Platforms
As we discussed in previous chapters, JSON is an excellent medium to communicate with web services and clients, whether the clients are web applications or traditional applications. This is especially true for mobile applications, many of which run over lower-bandwidth wide area networks, where JSON’s brevity in comparison with XML makes overall data payloads smaller, and thereby ensuring faster response time for remote queries.
Today’s leading mobile platforms are Android and iOS. Android, running a variant of Linux, supports software development in Java and includes a JSON processor in the org.json namespace. iOS, loosely derived from Mach and BSD, supports software development using Objective-C, Swift, C, and C++, although for most application development, you use Objective-C or Swift, each of which contains a binding to the NSJSONSerialization class, which implements JSON parsing and JSON serialization.
An additional option for mobile developers is to use a cross-platform toolkit, such as Qt, for application development. Qt runs on a variety of platforms, including Android, iOS, and BlackBerry. Qt defies the QJsonDocument and QJsonObject classes, which you can use to interconvert between maps and JSON. Qt is an open source framework that’s been around for many years, and runs not just on mobile platforms, but on Mac OS X, Windows, and Linux, as well as many other platforms.
The JSON we’ll discuss in the following sections is similar to what we’ve been using in the past chapters and is a document that looks like this:
Parsing JSON on Android
Android provides the JSONObject class, which lets you represent the name-value pairs of JSON documents through an interface that’s conceptually similar to a map, and includes serialization and deserialization through getter and setter methods that access the named filds of a JSON object.