Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

write json string to file in java

// Write JSON string to a file
try (FileWriter file = new FileWriter("fileName.json")) {
  file.write(data); // data is a JSON string here
  file.flush();
} catch (IOException e) {
  e.printStackTrace();
}
Comment

write json file java

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 )
    {
        //First Employee
        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);
         
        //Second Employee
        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);
         
        //Add employees to list
        JSONArray employeeList = new JSONArray();
        employeeList.add(employeeObject);
        employeeList.add(employeeObject2);
         
        //Write JSON file
        try (FileWriter file = new FileWriter("employees.json")) {
            //We can write any JSONArray or JSONObject instance to the file
            file.write(employeeList.toJSONString()); 
            file.flush();
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

PREVIOUS NEXT
Code Example
Java :: sum of arraylist java 8 
Java :: java create file if not exists 
Java :: solid principles in programming 
Java :: how to generate random number in java 
Java :: print * pattern in java 
Java :: java prime numbers 
Java :: how to initialise array in java without size 
Java :: export java home 
Java :: java add com.google.guava dependancy maven 
Java :: java string padding 
Java :: get random string from array list 
Java :: iterator java 
Java :: generate random color in java in android 
Java :: java convert LocalDateTime to long 
Java :: jsonarray to list java 
Java :: java convert string to int array 
Java :: java double format 
Java :: string remove last character 
Java :: initialize hashset with values java 9 
Java :: close a popup selenium python 
Java :: replace last char in string java 
Java :: sqlexception 
Java :: android studio constraint layout proportional height 
Java :: java text file to arraylist 
Java :: odd number in java 
Java :: java list distinct by attribute 
Java :: java return tuple 
Java :: string.join java 8 
Java :: concat result of List to one text java 
Java :: fileinputstream 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =