load json file with java code example
Example 1: 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 );
}
}
Example 2: how to read from json files in java
package com.howtodoinjava.demo.jsonsimple;
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)
{
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("employees.json"))
{
Object obj = jsonParser.parse(reader);
JSONArray employeeList = (JSONArray) obj;
System.out.println(employeeList);
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)
{
JSONObject employeeObject = (JSONObject) employee.get("employee");
String firstName = (String) employeeObject.get("firstName");
System.out.println(firstName);
String lastName = (String) employeeObject.get("lastName");
System.out.println(lastName);
String website = (String) employeeObject.get("website");
System.out.println(website);
}
}
Example 3: java load .json file
package com.howtodoinjava.demo.jsonsimple;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class WriteJSONExample
{
@SuppressWarnings("unchecked")
public static void main( String[] args )
{
JSONObject employeeDetails = new JSONObject();
employeeDetails.put("firstName", "Lokesh");
employeeDetails.put("lastName", "Gupta");
employeeDetails.put("website", "howtodoinjava.com");
JSONObject employeeObject = new JSONObject();
employeeObject.put("employee", employeeDetails);
JSONObject employeeDetails2 = new JSONObject();
employeeDetails2.put("firstName", "Brian");
employeeDetails2.put("lastName", "Schultz");
employeeDetails2.put("website", "example.com");
JSONObject employeeObject2 = new JSONObject();
employeeObject2.put("employee", employeeDetails2);
JSONArray employeeList = new JSONArray();
employeeList.add(employeeObject);
employeeList.add(employeeObject2);
try (FileWriter file = new FileWriter("employees.json")) {
file.write(employeeList.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}