Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java load .json file

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.commons.io.IOUtils; 

    public class JsonParsing {

        public static void main(String[] args) throws Exception {
            InputStream is = 
                    JsonParsing.class.getResourceAsStream( "sample-json.txt");
            String jsonTxt = IOUtils.toString( is );

            JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );        
            double coolness = json.getDouble( "coolness" );
            int altitude = json.getInt( "altitude" );
            JSONObject pilot = json.getJSONObject("pilot");
            String firstName = pilot.getString("firstName");
            String lastName = pilot.getString("lastName");

            System.out.println( "Coolness: " + coolness );
            System.out.println( "Altitude: " + altitude );
            System.out.println( "Pilot: " + lastName );
        }
    }
Comment

read json file 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 :: all devisor of a number java 
Java :: How to perform a breadth first search through a binary tree, in Java? 
Java :: android studio remove action bar 
Java :: how to create a circle in java 
Java :: java calendar hour vs hour of day 
Java :: java xmx example 
Java :: loop through string in java 
Java :: sorting an arraylist 
Java :: what language is android written in 
Java :: java foreach 
Java :: jframe set visibility 
Java :: how to print multiple things in one line java 
Java :: android check if app is running 
Java :: circular textview android 
Java :: check instance of java 
Java :: valueof vs tostring 
Java :: runnable interface in java 
Java :: create stream from array java 
Java :: javafx tableview add data 
Java :: list of lists java 
Java :: java array to collection 
Java :: How to swap two values in Java using a supporting method? 
Java :: Create a button that redirects to another activity in android studio 
Java :: How to find the second smallest value in an array without sorting it in Java? 
Java :: Java Stack class peek() method 
Java :: java srting array to string 
Java :: How to determine if a binary tree has a certain root to leaf target sum value, in Java? 
Java :: java float 0/0 
Java :: split number java 
Java :: java get JComboBox value 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =