showing results for - "how to read from json files in java"
Mats
17 Jan 2016
1package com.howtodoinjava.demo.jsonsimple;
2 
3import java.io.FileNotFoundException;
4import java.io.FileReader;
5import java.io.IOException;
6 
7import org.json.simple.JSONArray;
8import org.json.simple.JSONObject;
9import org.json.simple.parser.JSONParser;
10import org.json.simple.parser.ParseException;
11 
12public class ReadJSONExample 
13{
14    @SuppressWarnings("unchecked")
15    public static void main(String[] args) 
16    {
17        //JSON parser object to parse read file
18        JSONParser jsonParser = new JSONParser();
19         
20        try (FileReader reader = new FileReader("employees.json"))
21        {
22            //Read JSON file
23            Object obj = jsonParser.parse(reader);
24 
25            JSONArray employeeList = (JSONArray) obj;
26            System.out.println(employeeList);
27             
28            //Iterate over employee array
29            employeeList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );
30 
31        } catch (FileNotFoundException e) {
32            e.printStackTrace();
33        } catch (IOException e) {
34            e.printStackTrace();
35        } catch (ParseException e) {
36            e.printStackTrace();
37        }
38    }
39 
40    private static void parseEmployeeObject(JSONObject employee) 
41    {
42        //Get employee object within list
43        JSONObject employeeObject = (JSONObject) employee.get("employee");
44         
45        //Get employee first name
46        String firstName = (String) employeeObject.get("firstName");    
47        System.out.println(firstName);
48         
49        //Get employee last name
50        String lastName = (String) employeeObject.get("lastName");  
51        System.out.println(lastName);
52         
53        //Get employee website name
54        String website = (String) employeeObject.get("website");    
55        System.out.println(website);
56    }
57}
58
queries leading to this page
java read a json filehow to read json object from file in javahow to save json data in java using collectionreading data from json file in javahow to make java read a json filejava parsing json filejava write to json filejsonobject read a file in javajava json how to open and read json fileread json file javaread json file as json string javaparse json from file javareading a json file in javajson java write to filejava get string from json filehow to read json files using javaopen json file javadecode json javajson file read in javahow to read an element in a json array from file in javajava parse file jsonjava read json stringhow to read into java from jsonread json in javaread json file jvahow to open json file in javaread in json file javahandling json file in jvadecode json file in java tutorialread json file as json object javajava org json read filejson encoder decoder javajsonobject read javacreate json file javajava open json fileimport json variables to javaread from json file java and convert itparse json file in javafile json javajava use jackon to read json filejava read in json filehow to read json data from text file in javajava read a file as jsonjava reading json fileuse json file in javaread json javajava read json file in folderread jsockson from file javahow to read jason file data in class file in javareading from a json file in javaread json file in javahow to read json file to string in javajava read in jsondecode json result javajava read json fielhow to read a json file in javajava how to open and read json fileparse json files javaread json file data in javaread json file from directory javaorg json java read fileread json file from directory java 8reading json file in javahow to decode a json file in javaread json file from package javahow to extract data from json file in javacreate json file in javaread local json file in javajava how to read in a json filejava load json filehow to parse and load json files in javajson file to json object javajava json org decodejava json filejava read json file to json stringhow to make json encoder and decoder in javawriting a basic java json parserhow to read json in javasave json array to json file javause json file javajava how to write a json fileread json object in javajson from file javajava read jsonread json properties file javaread json from a file javahow to read json data in javajava json read from fileread from json file javajava read json from file to objectjson encoder 2c decoder javajava get json string from fileread a json file that start 5b javaaccessing json file in javahow to read json object from text file in javahow to read json file using javareadwrite to jsonobjectjava get json from fileread a specific object from json file javajava json object from filejava read json file to stringjson reader in javareading json javahow to use java to save a jsonjava json readjson read javadecoding json in javajava make application read json fileparse json file javaload json from file javaparsing a json file in javajava read from json filereturn json file javaread json file as object javahow to read json objects from file in javajava use json fileread json from file javahow to read json file in javahow to read value from json file in javajava access json filejava read json filejson file javaread json file and java objectsjava read json from fileread a json in javagetting data from json file javahow to extract data from json file javahow to read json from fole javadecode json in javajson decode javahow to read json string in javajava how to read json filehow to read json object from string in javajava read json filesextract json file javajava create json filejava write and read json filejava decode jsonhow to read json objects in javaread json string from text file in javaread json file java wtih jackonhow to read json file with javareading json file from resource folder in javajava parse json filecan you read json data in javajava read from jsonread a json file and convert to java objecthow to read json javajava read json file and get data from the whole objecthow to read from json files in java