Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Generating JSON on Android


JSONObject also supports setter methods to initialize data in a JSON map. With these methods, you can assign data to a JSON object and then get the JSON representation by invoking its toString method.

How to do it…

Here's a simple example:

import org.JSON.JSONObject;

JSONObject data = new JSONObject();
data.put("call", "kf6gpe-7");
data.put("lat", 37.40150);
data.put("lng", -122.03683);
String json = data.toString();

How it works…

The polymorphic put method can take an integer, long integer, object, Boolean, or double, assigning the slot you name the value you specify.

The JSONObject class defines the toString method, which takes an optional number of spaces to indent nested structures for pretty-printed JSON. If you don't pass this indent, or pass 0, the implementation encodes the JSON in as compact a manner as possible.

There's more…

There's also the putOpt method, which takes any subclass of Object, and puts the value to the name if both the name and value are non...