file write in json file in java

Solutions on MaxInterview for file write in json file in java by the best coders in the world

showing results for - "file write in json file in java"
Charline
18 Feb 2019
1import java.io.FileWriter;
2import java.io.IOException;
3import org.json.simple.JSONObject;
4public class CreatingJSONDocument {
5   public static void main(String args[]) {
6      //Creating a JSONObject object
7      JSONObject jsonObject = new JSONObject();
8      //Inserting key-value pairs into the json object
9      jsonObject.put("ID", "1");
10      jsonObject.put("First_Name", "Shikhar");
11      jsonObject.put("Last_Name", "Dhawan");
12      jsonObject.put("Date_Of_Birth", "1981-12-05");
13      jsonObject.put("Place_Of_Birth", "Delhi");
14      jsonObject.put("Country", "India");
15      try {
16         FileWriter file = new FileWriter("E:/output.json");
17         file.write(jsonObject.toJSONString());
18         file.close();
19      } catch (IOException e) {
20         // TODO Auto-generated catch block
21         e.printStackTrace();
22      }
23      System.out.println("JSON file created: "+jsonObject);
24   }
25}