Consuming a JSON response in Android
JSON's basic types are:
* Number (integer, real or floating point)
* String (double-quoted Unicode with backslash escaping)
* Boolean (true and false)
* Array (an ordered sequence of values, comma-separated and enclosed in square brackets)
* Object (collection of key: value pairs, comma-separated and enclosed in curly braces)
This article gives an insight about how to use JSON as a data interchange format, provided with the URL of a Restful Service which returns a JSON response.
Advantages of JSON
* Simpler than XML because it is not a markup language and a natural representation of data
* JSON is better data exchange format; XML is a better document exchange format
* JSON is easier to read for machines with no/thin client-side library
* JSON is a natural fit for data consumption by browser clients, for example Ajax components
* Ability to represent general data structures: records, lists and trees
* Parsing and generating JSON support in 21 languages
Disadvantages of JSON
*JSON does not have a <[CDATA]> feature, so it is not well suited to act as a carrier of sounds or images or other large binary payloads.
* Unlike XML, JSON does not provide any display capabilities because it is not a document markup language. JSON was not even intended for that purpose.
* JSON is not extensible - it does not need to be because it's not a document markup language.
JSON is a newer format so not many tools as yet to help with authoring & parsing , however, some available ones are:
*JSON Tools - Java Tools for the JSON Format (parser, renderer, serializer, mapper, validator)
*JSON-lib - Java library for transforming beans, maps, collections, java arrays and XML to JSON and back again to beans.
*JSON in Java - Java APIs from json.org (see more below)
*JSON-taglib - JSON-taglib is a JSP 2.0 tag library used to render JSON data from within JSP code.
Comparison of JSON with XML
XML is simpler than SGML, but JSON is much simpler than XML. JSON has a much smaller grammar and maps more directly onto the data structures used in modern programming languages.
JSON is not extensible because it does not need to be. JSON is not a document markup language, so it is not necessary to define new tags or attributes to represent data in it.
JSON has the same interoperability potential as XML.
JSON is at least as open as XML, perhaps more so because it is not in the center of corporate/political standardization struggles.
Attached is a small sample as to how to use JSON as a data interchange format in Android.
The activity hits a JSON URL and gets the response as a stream. The response is consumed as HTTPEntity (provided by Apache and comes with the Android SDK) and the input stream is the extracted from the entity.
And the response is converted into a String. This string is then cast into a JSONObject.
String result = convertStreamToString(instream);
Log.i("REST: result", result);
JSONObject json = new JSONObject(result);
This response helps us to manipulate it in a dynamic manner. The response JSONObject contains a key/value pair which is consumed and printed in the log as JSONArray.
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
This sample would need you to see the results in the log as to how a JSON response can be obtained as a name value pair and then bind to the UI as a dynamic text/image etc.