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

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 :: highlight selected item in recyclerview android 
Java :: How to perform breadth first search through a binary tree, in Java? 
Java :: open file java 
Java :: java create arraylist 
Java :: how to make int array java android 
Java :: get absolute path from relative java 
Java :: get imei programmatically android 
Java :: how to add elements to an empty array in java 
Java :: java scanner 
Java :: cosinus-1 java 
Java :: set visible gui java 
Java :: android date time 
Java :: java regex replace all characters before 
Java :: java string format thousand separator 
Java :: java datetime now 
Java :: how to read input in java 
Java :: spigot send player action bar message 1.8 
Java :: count vowels in java 
Java :: android java xml combo box 
Java :: why are there no destructors in java? 
Java :: how to read a excel file in java 
Java :: java summe array 
Java :: flow dependency android 
Java :: Java how to make a number with digits 
Java :: how to solve Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
Java :: java install in fedora 
Java :: max heap java 
Java :: java date format with timezone 
Java :: print java 
Java :: how to check the current user in firebase android 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =