Question: In need helping JAVA . Implement code for the Converter class not Main class Thank you in advance . Converter Class : import java.io.*; import

In need helping JAVA . Implement code for the Converter class not Main class Thank you in advance .

Converter Class :

import java.io.*; import java.util.*; import com.opencsv.*; import org.json.simple.*; import org.json.simple.parser.*;

public class Converter { /* Consider the following CSV data: "ID","Total","Assignment 1","Assignment 2","Exam 1" "111278","611","146","128","337" "111352","867","227","228","412" "111373","461","96","90","275" "111305","835","220","217","398" "111399","898","226","229","443" "111160","454","77","125","252" "111276","579","130","111","338" "111241","973","236","237","500" The corresponding JSON data would be similar to the following (tabs and other whitespace have been added for clarity). Note the curly braces, square brackets, and double-quotes! These indicate which values should be encoded as strings, and which values should be encoded as integers! { "colHeaders":["ID","Total","Assignment 1","Assignment 2","Exam 1"], "rowHeaders":["111278","111352","111373","111305","111399","111160", "111276","111241"], "data":[[611,146,128,337], [867,227,228,412], [461,96,90,275], [835,220,217,398], [898,226,229,443], [454,77,125,252], [579,130,111,338], [973,236,237,500] ] } Your task for this program is to complete the two conversion methods in this class, "csvToJson()" and "jsonToCsv()", so that the CSV data shown above can be converted to JSON format, and vice-versa. Both methods should return the converted data as strings, but the strings do not need to include the newlines and whitespace shown in the examples; again, this whitespace has been added only for clarity. NOTE: YOU SHOULD NOT WRITE ANY CODE WHICH MANUALLY COMPOSES THE OUTPUT STRINGS!!! Leave ALL string conversion to the two data conversion libraries we have discussed, OpenCSV and json-simple. See the "Data Exchange" lecture notes for more details, including example code. */ @SuppressWarnings("unchecked") public static String csvToJson(String csvString) { String results = ""; try { CSVReader reader = new CSVReader(new StringReader(csvString)); List full = reader.readAll(); Iterator iterator = full.iterator(); // INSERT YOUR CODE HERE } catch(Exception e) { return e.toString(); } return results.trim(); } public static String jsonToCsv(String jsonString) { String results = ""; try {

StringWriter writer = new StringWriter(); CSVWriter csvWriter = new CSVWriter(writer, ',', '"', ' '); // INSERT YOUR CODE HERE } catch(Exception e) { return e.toString(); } return results.trim(); }

}

Main Class :

import java.io.*;

public class Main { public static void main(String[] args) { ClassLoader loader = ClassLoader.getSystemClassLoader(); // Get CSV Data StringBuilder csvFile = new StringBuilder(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(loader.getResourceAsStream("resources" + File.separator + "grades.csv"))); String line; while((line = reader.readLine()) != null) { csvFile.append(line).append(' '); } } catch(IOException e) { e.printStackTrace(); } String csvFileString = csvFile.toString().trim(); // Get JSON Data StringBuilder jsonFile = new StringBuilder(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(loader.getResourceAsStream("resources" + File.separator + "grades.json"))); String line; while((line = reader.readLine()) != null) { jsonFile.append(line).append(' '); } } catch(IOException e) { e.printStackTrace(); } String jsonFileString = jsonFile.toString().trim(); // Convert CSV to JSON; Print Results to Console System.out.println("CONVERSION RESULTS (CSV to JSON)"); System.out.println("================================");

String json = Converter.csvToJson(csvFileString); System.out.println(json); // Convert JSON to CSV; Print Results to Console System.out.println(" CONVERSION RESULTS (JSON to CSV)"); System.out.println("================================"); String csv = Converter.jsonToCsv(jsonFileString); System.out.println(csv); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!