JavaScript JSON Cookbook

JavaScript JSON Cookbook

English | 2015 | ISBN: 978-1-78528-690-2 | 192 Pages | PDF, EPUB, MOBI | 10 MB


JSON (JavaScript Object Notation) is a lightweight text-based data interchange format used to create objects to transfer data over the Internet. It’s widely used today by common web applications, as well as mobile applications.
This book gives you clear ways that you can exchange objects using JSON, regardless of whether you’re developing a web or traditional networked application. You’ll start with a brief refresher on JSON and JavaScript syntax and read and write on the client and server. Then, you’ll learn how to use JSON in simple AJAX applications using AngularJS and jQuery. Next, you will learn how to exchange objects with databases using MongoDB and CouchDB. You’ll also explore how to use JSON in a type-safe manner, writing programs that have fewer bugs.

+

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.