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 :: println java 
Java :: android change checkbox color 
Java :: how to do stuff with a scoreboard minecraft 
Java :: java string to double 
Java :: java obtain list string from list object 
Java :: java join list as string 
Java :: @data lombok 
Java :: get current unix timestamp java 
Java :: turn java code to .exe 
Java :: how to access variable from another class in java 
Java :: android recyclerview show hide item with animation with default animation 
Java :: euclids algoritm java gcd 
Java :: imageio class java 
Java :: java template string 
Java :: how to calculate exponential in java 
Java :: how to read file from assets folder in android 
Java :: java datetime now 
Java :: flutter remove last caracter from string 
Java :: public String toString() { 
Java :: how to use input in java 
Java :: what is use of static block in java 
Java :: view binding in recyclerview adapter android java 
Java :: android studio listview arrayadapter 
Java :: how to summon lightning in minecraft 
Java :: prepared statement update 
Java :: java stream distinct by key 
Java :: string to byte array java utf-8 
Java :: java loop over string 
Java :: java.math.biginteger cannot be cast to java.lang.long 
Java :: get request in java 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =