pvillega’s posterous

pvillega’s posterous

Pere Villega  //  Born in Barcelona, living in Dublin, and tagged as geek since youth. Developer in the path to becoming a software architect. I swear this is not a proper blog :)

Feb 16 / 7:22am

JSON

I'm personally not a fan of XML. I know its useful but since the abuse it suffered in the hands of Java framework developers (see Spring and Struts) I though it was a horrible format for configuration, too verbose. The raise of JSON and other alternative formats like YAML seems to prove me right. According to the Wikipedia, JSON (JavaScript Object Notation) is a lightweight computer data interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (called objects). The JSON format is often used for transmitting structured data over a network connection in a process called serialization. Its main application is in Ajax web application programming, where it serves as an alternative to the use of the XML format. The JSON format is specified in RFC 4627 by Douglas Crockford. The official Internet media type for JSON is application/json. The JSON file extension is .json. In this post I will look at JSON and how to interact with it using Java. JSON JSON purpose is to transmit data without the bloating related to some other formats. To that end it uses a really small set of data. 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
  • null

An example of a JSON structure (Object) representing a person is:

{

"firstName": "John",

"lastName": "Smith",

"address": {

"streetAddress": "21 2nd Street",

"city": "New York",

"state": "NY",

"postalCode": 10021

},

"phoneNumbers": [

"212 555-1234",

"646 555-4567"

]

}

As you can see is an straightforward format that requires few explanation. JSON and Java Although JSON was thought for Javascript, its growing usage on AJAX and other services makes mandatory to have some tools to work with it using Java. AS always the open source community has answered the needs and we have several interesting libraries available:

  • Official JSON implementation: provides classes to create JSON objects manually or to parse a JSON string into the corresponding objects. It also provides some classes to convert between JSON and other formats like XML.
  • Gson: converts from JSON to Java classes and the other way around, accepting Generics and other Java 1.5 features.
  • JSON tools: another library to parse, validate and serialize JSON objects

Those 3 libraries seem to be the most complete ones and should be enough for all your purposes, although if you search on Google you will find lots of alternatives.

Loading mentions Retweet

Filed under // java json

Comments (0)