Java json

Java Json Parser

javax prepares interface for json parser, but by default we cannot use json parser.
Java 8 ? does not support json operation by default.

javax.json-api

This is one of easy way to parse json.

Get started

Please add following 2 dependencies.

compile group: 'org.glassfish', name: 'javax.json', version: '1.1.2'
compile group: 'javax.json', name: 'javax.json-api', version: '1.1.2'    

The details :
Package javax.json
javax.json-api is just interface, actual implementation is in glassfish javax.json.

Example

After import above libraries, you can use json-api.

Target json(example)

{
  "data":
  {
    "Tom": {
      "Mission: Impossible": "5.0",
      "Avater": "4.5",
      "The Empire Strikes Back": "1.5",
      "The Lord of the Rings": "2.0",
      "Fahrenheit 9/11": "3.5"
    },
    "Michael": {
      "The Empire Strikes Back": "4.5",
      "Titanic": "2.0",
      "Dragonball": "4.5",
      "The Lord of the Rings": "2.0",
      "Fahrenheit 9/11": "5.0",
      "Moneyball": "3.5",
      "Avater": "3.0",
      "Mission: Impossible": "1.0"
    }
}
public void read() {

        FileInputStream fis = null;

        try {
            fis = new FileInputStream(filename);
            JsonReader reader = Json.createReader(fis);
            JsonObject root = reader.readObject();
            JsonObject members = root.getJsonObject("data");

            for (String name : members.keySet()) {
                JsonObject reviewsjson = members.getJsonObject(name);

                for (String key : reviewsjson.keySet()) {
                    String value = reviewsjson.getString(key);   // double value
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch(IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }