dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
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
 
Thanks for your registration, follow us on our social networks to keep up-to-date