Question: I am working in Java trying to find out how to convert the following Hashmap to JSON. I don't know how to do this, however.
I am working in Java trying to find out how to convert the following Hashmap to JSON. I don't know how to do this, however. What would be the best way to do this given the following Java code: public class ServerAnalyzerBackEnd { private String url; private boolean validURL; private ArrayList dataToFetch; public ServerAnalyzerBackEnd(String url) { this.url = url; this.validURL = validateURLSyntax(); this.dataToFetch = new ArrayList<>(); } // Add all data pieces to fetch public void fetchAll() { dataToFetch.add(new ResponseHeaderData()); dataToFetch.add(new CertChainData()); dataToFetch.add(new CipherSuiteData()); } //add a single data piece to be fetched public void addDataPieceToFetch(DataPiece dataPiece) { dataToFetch.add(dataPiece); } public Map createOutput(){ String host; Map params = new HashMap<>(); params.put("url", url); String secureURL = ""; URL testURL; int port; String cert_chain = ""; if (!validURL) { System.out.println("Invalid URL syntax."); returnErrorJson(); } //url is not null, assign to secureURL to create https connection if(url != null){ secureURL = url; } //Initialize sesson and connection SSLSession session = null; HttpsURLConnection connection = null; //create URL object from 'url' //create https connection based off of url and then call getCertChain method to pull that cert chain try { testURL = new URL(secureURL); port = testURL.getPort(); host = testURL.getHost(); SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); Socket socket = socketFactory.createSocket(host, port); session = ((SSLSocket) socket).getSession(); connection = (HttpsURLConnection)testURL.openConnection(); } catch(UnknownHostException e){ System.out.println("This ip address could not be determined"); returnErrorJson(); } catch(MalformedURLException e){ e.printStackTrace(); returnErrorJson(); } catch(IOException e){ e.printStackTrace(); } // fetch data for all data pieces for (DataPiece dataPiece : dataToFetch) { dataPiece.fetchData(connection, session); } //put all data pieces into hashmap for (DataPiece dataPiece : dataToFetch) { params.put(dataPiece.getDataName(), dataPiece.getDataResult()); } // return the hashmap return params; } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
