write java code for the following The software shall print t
write java code for the following.
The software shall print the list of all product names and IDs to the screen.
The software shall save the list of all product names and IDs to a JSON file.
Solution
Please follow the code and comments for description :
CODE :
import java.io.File; // required imports
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JSONObjWriteToFileEx { // class to run the code
public static void main(String[] args) { // driver method
Scanner sc = new Scanner(System.in); // scanner class to get the data
JSONObject productObj = new JSONObject(); // calling the JSONObject class to create a object class
System.out.println(\"Please Enter the list of Product Names (\'E\' to exit) : \"); // prompt for the user to enter the data
JSONArray listOfProNames = new JSONArray(); // calling the JSONArray class to create a object class arrays
while (true) { // iterating over the data entered
String names = sc.nextLine(); // get the data
if (names.equalsIgnoreCase(\"E\")) { // check for the data
break;
}
listOfProNames.add(names); // add to the list
}
productObj.put(\"Product Names\", listOfProNames); // add to the JSON object
System.out.println(\"Please Enter the list of Product ID\'s (\'E\' to exit) : \"); // prompt for the user to enter the data
JSONArray listOfProId = new JSONArray(); // calling the JSONArray class to create a object class arrays
while (true) { // iterating over the data entered
String id = sc.nextLine(); // get the data
if (id.equalsIgnoreCase(\"E\")) { // check for the data
break;
}
listOfProId.add(id); // add to the list
}
productObj.put(\"Product ID\'s\", listOfProId); // add to the JSON object
try { // try catch block of code
File file = new File(\"JsonFile.json\"); // Writing to a file
file.createNewFile(); // create a file if not any
FileWriter fileWriter = new FileWriter(file); // writer class to write the data
System.out.println(\"Writing the JSON object to file\"); // message to the user
System.out.println(\"---------------------------------\");
System.out.print(productObj); // print data to console
fileWriter.write(productObj.toJSONString()); // write the data to the file
fileWriter.flush(); /// flush the data
fileWriter.close(); // close the file
} catch (IOException e) { //catch the exception
System.out.println(e.getMessage()); // print to console
}
}
}
NOTE :
Please add the respective jar file to the project, you use for the data and the code to run without errors and bugs. I have used the json-simple-1.1.1.jar file.
OUTPUT :
Please Enter the list of Product Names (\'E\' to exit) :
rice
wheat
maize
chana flour
wheat flour
e
Please Enter the list of Product ID\'s (\'E\' to exit) :
1
2
3
4
5
e
Writing the JSON object to file
---------------------------------
{
\"Product ID\'s\":[\"1\",\"2\",\"3\",\"4\",\"5\"],
\"Product Names\":[\"rice\",\"wheat\",\"maize\",\"chana flour\",\"wheat flour\"]
}
JsonFile.json :
{
\"Product ID\'s\":[\"1\",\"2\",\"3\",\"4\",\"5\"],
\"Product Names\":[\"rice\",\"wheat\",\"maize\",\"chana flour\",\"wheat flour\"]
}
Hope this is helpful.

