Home » Tip Bank » Java
  • Comment on this Article
Language: Java
Expertise: Intermediate
Apr 13, 2020

Reading a File via GSON

The following example reads a JSON file via JSON:

// create Gson instance
Gson gson = new Gson();

// create a Reader
Reader reader = Files.newBufferedReader(Paths.get("myfile.json"));

// JSON file to map
Map map = gson.fromJson(reader, Map.class);

// print map
for (Map.Entry entry : map.entrySet()) {
   System.out.println(entry.getKey() + "=" + entry.getValue());
}

// close reader
reader.close();
Octavia Anghel
 
Submit a Tip
Browse "Java" Tips
Browse All Tips
eloquentjavascript.net
htmlgoodies.com