Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m using Intellij for a project using Java. I have included the following code

ID: 3749810 • Letter: I

Question

 I'm using Intellij for a project using Java. I have included the following code. I need to know how to convert HashMap to JSON. How can I do this?  //Response Header Imports import java.net.*; import java.util.*; import java.io.IOException; ///////////////////////////////// import com.commercebank.serveranalyzer.BackEnd.DataPieces.CertChainData; import com.commercebank.serveranalyzer.BackEnd.DataPieces.CipherSuiteData; import com.commercebank.serveranalyzer.BackEnd.DataPieces.DataPiece; import com.commercebank.serveranalyzer.BackEnd.DataPieces.ResponseHeaderData; import org.springframework.web.servlet.ModelAndView;  import java.util.Map;  import java.security.cert.X509Certificate; import java.net.URL; import javax.net.ssl.*; import java.security.cert.Certificate; import java.net.MalformedURLException;  public class ServerAnalyzerBackEnd {      private String url;     private boolean validURL;      private ArrayList<DataPiece> 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 ModelAndView start() {         String host;          Map<String, Object> params = new HashMap<>();         params.put("url", url);         String secureURL = "";         URL testURL;         int port;         String cert_chain = "";          if (!validURL)         {             System.out.println("We should probably stop it here or something.");         }         //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(MalformedURLException e){             e.printStackTrace();         }         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 new ModelAndView("showURL", params);     }      public boolean validateURLSyntax()     {         int firstColon;         int secondColon;         boolean URLsyntax = true;          //Checks if the first four characters are http         if (!this.url.substring(0,4).equals("http"))         {             URLsyntax = false;         }          //Checks if a port number was specified. If not, appends :443 to the URL.         firstColon = this.url.indexOf(":");         secondColon = this.url.lastIndexOf(':');          if (secondColon == -1 || firstColon == secondColon)         {             this.url = this.url + ":443";         }          //Something like https://www.google.com: and no port actually specified or junk is after the port.         secondColon = this.url.lastIndexOf(':');         if (this.url.charAt(this.url.length() - 1) == ':' || !Character.isDigit(this.url.charAt(secondColon + 1)))         {             URLsyntax = false;         }         return URLsyntax;     }  }

Explanation / Answer

Map<String,Object> mapping = new HashMap<>();
String[] value_1 = new String[] { "value11", "value12", "value13" };
String[] value_2 = new String[] { "value21", "value22", "value23" };
mapping.put("key1", value_1);
mapping.put("key2", value_2);
mapping.put("key3","string1");
mapping.put("key4","string2");

String json = new ObjectMapper().writeValueAsString(mapping);
System.out.println(json);
Maven Dependencies for Jackson :

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.3</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
<scope>compile</scope>
</dependency>
If you are using JSONObject library, you can convert mapping to JSON as follows:

mapping<String, Object> mapping = new HashMap<>();
String[] value_1 = new String[] { "value11", "value12", "value13" };
String[] value_2 = new String[] { "value21", "value22", "value23" };
mapping.put("key1", value_1);
mapping.put("key2", value_2);

JSONObject json = new JSONObject(mapping);
System.out.println(json);
Maven Dependencies for JSONObject :

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>