Thoughts by Techxplorer

Thoughts on my experiences with technology

Generating JSON in Java

By: qthomasbower

As part of my work on the AusStage website I’ve been asked to explore ways in which website authors could readily access data housed in the AusStage database for inclusion on their own websites. The easiest way to accomplish this, I believe, is to provide website authors with an AJAX based library that would allow them to access certain defined subsets of data. For example an organisation may wish to list all of the events that they’ve been involved in on their website and they could use this service to achieve that.

In a way the service can be seen as the first step in supporting the creation of mashups of AusStage data with potentially other data as well.

One of the data formats that the service outputs is the JavaScript Object Notation (JSON) format. The other formats are HTML, in the form of an unordered list, or in XML. With these three formats the majority of use cases can be supported.

The issue I faced was how to generate the JSON code for an array of items that held event details. A search of the Interweb showed that there are a number of libraries and the easiest one, in my opinion, to use was the JSON.simple library. To achieve my desired outcome I create a JSON array, and then populate it with a series of JSON objects.

The only potential problem with the library is that it, at least on my system, generates unchecked exceptions when adding items to its internal data structures. I believe this has something to do with the use classes within the Collections Framework that don’t explicitly declare that they are going to be used for storing string objects.

Regardless they seem to be harmless errors as the library is so far working brilliantly and providing me with exactly the functionality that I required.

The image “Compositing The Creative Commons” was uploaded to Flickr by qthomasbower and used under the terms of a Creative Commons License.

Category: Thoughts
Tag: