[Java] Read JSON from file in Eclipse

I found myself searching for read file in java each time i need it. It think it is a good idea to write I down on my blog as a note.

Simple, import filename.json your eclipse project by drag and drop.

Call the method:

JSONObject results = readJsonFromFileWithFileName("filename.json");

The actual function

private JSONObject readJsonFromFileWithFileName(String fileName) {
JSONParser parser = new JSONParser();
JSONObject jsonObject = new JSONObject();
try {
   URL url = Test.class.getClassLoader().getResource(fileName);
   System.out.println(url.getPath());
   Object obj = parser.parse(new FileReader(url.getPath()));
   jsonObject = (JSONObject) obj;
} catch (FileNotFoundException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
} catch (org.json.simple.parser.ParseException e) {

// TODO Auto-generated catch block
   e.printStackTrace();
}
   System.out.println("here" + jsonObject);
   returnjsonObject;
}