JSONWriter class used to write json object to file. Its very easy.

Step1: Create s FileWriter object.

Step2: Create JSONWrite object and pass FileWrite object.

Step3: Repeat step2 for as per requirement.

Step4: Close the FileWriter object.

 


import java.io.FileWriter;
import java.io.IOException;

import org.json.JSONWriter;

public class FileWrite {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try {
			FileWriter fw=new FileWriter("d://demo.json");
		
			new JSONWriter(fw)
		     .object()
		         .key("name")
		         .value("Chiti Robo!")
		     .endObject();
			
			fw.close();
			System.out.println("file write done");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

Note: We can skip the endObject method in JsonWriter . But it won't close the close the json object with curly(just for fun)