Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to read a JSON file in java

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
 
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
 
public class ReadJSONExample 
{
    @SuppressWarnings("unchecked")
    public static void main(String[] args) 
    {
        //JSON parser object to parse read file
        JSONParser jsonParser = new JSONParser();
         
        try (FileReader reader = new FileReader("employees.json"))
        {
            //Read JSON file
            Object obj = jsonParser.parse(reader);
 
            JSONArray employeeList = (JSONArray) obj;
            System.out.println(employeeList);
             
            //Iterate over employee array
            employeeList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
 
    private static void parseEmployeeObject(JSONObject employee) 
    {
        //Get employee object within list
        JSONObject employeeObject = (JSONObject) employee.get("employee");
         
        //Get employee first name
        String firstName = (String) employeeObject.get("firstName");    
        System.out.println(firstName);
         
        //Get employee last name
        String lastName = (String) employeeObject.get("lastName");  
        System.out.println(lastName);
         
        //Get employee website name
        String website = (String) employeeObject.get("website");    
        System.out.println(website);
    }
}

//Update pom.xml with json-simple maven dependency.
Comment

PREVIOUS NEXT
Code Example
Java :: jlabel text grösse 
Java :: getBatteryPercentage android studio 
Java :: hybrid inheritance in java 
Java :: android studio call on a string 
Java :: how to set to nothing a ComboBox in java 
Java :: matrix program 
Java :: sort array from certain index java 
Java :: add dynamic view in android from xml 
Java :: Magic square java user input 
Java :: java lambda expressions qunado foi implantada 
Java :: convert jython object to java object 
Java :: extracting kubernetes podname from java 
Java :: javafx line chaart duplicate series added 
Java :: java minimize all windows 
Java :: spring generate banner 
Java :: advantages of iterator in java 
Java :: how to find length of string array java 
Java :: how to read space separated characters in java 
Java :: how to find root viewGroop 
Java :: cancel block event spigot 
Java :: android studio analyze apk 
Java :: search in jdbc 
Java :: variables en java 
Java :: void add method using collections 
Java :: connect 2 package in android 
Java :: how to convert string to space separated int in java 
Java :: object class of java 
Java :: how to set slected row color in javafx tableview 
Java :: How to set the java.library.path from Eclipse 
Java :: ring check if the operating system is Android or not 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =