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 :: random number between 1 and 100 java 
Java :: foreach map java 
Java :: java foreach list 
Java :: check array is sorted java 
Java :: murtaza jafari 
Java :: size of queue in java 
Java :: Service vs Intent Service 
Java :: android hide system bar programmatically 
Java :: java print two dimensional array 
Java :: convert java to kotlin online editor 
Java :: java create new thread 
Java :: Failed to determine a suitable driver class 
Java :: string array to arraylist 
Java :: java print array as string 
Java :: how to make an int into a string java 
Java :: get type of variable java 
Java :: java join list as string 
Java :: Calculator Program in Java. 
Java :: war file vs jar file 
Java :: java random primary key 
Java :: arrays sort 2d array java 
Java :: Unsupported Modules Detected 
Java :: pass list to intent in android java 
Java :: check if string contains only letters java 
Java :: initialize hashset with values java 9 
Java :: swing setbounds 
Java :: why are there no destructors in java? 
Java :: java arraylist print 
Java :: how to summon lightning in minecraft 
Java :: get driver mysql 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =