So I am trying to use the API here: https://clinicaltables.nlm.nih.gov/apidoc/icd10cm/v3/doc.html#output
This is my code so far:
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;
public class DiagnosesAPIHelper
{
public static void main(String[] args)
{
try
{
URL url = new URL ("https://clinicaltables.nlm.nih.gov/api/icd10cm/v3/search?sf=code,name&terms=tuberc");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
if(responseCode != 200)
{
throw new Exception("HTTP Response code: " + responseCode);
}
else
{
String inline = "";
Scanner scanner = new Scanner(url.openStream());
//Write all the JSON data into a string using a scanner
while (scanner.hasNext()) {
inline += scanner.nextLine();
}
//Close the scanner
scanner.close();
//Using the JSON simple library parse the string into a json object
JSONParser parse = new JSONParser();
JSONArray jsonArray = (JSONArray) parse.parse(inline);
System.out.println(jsonArray.get(3).toString());
}} catch (Exception e) {
e.printStackTrace();
}
}
}
And here are the outputs that I am getting:
[["A15.0","Tuberculosis of lung"],["A15.4","Tuberculosis of intrathoracic lymph nodes"],["A15.5","Tuberculosis of larynx, trachea and bronchus"],["A15.6","Tuberculous pleurisy"],["A15.7","Primary respiratory tuberculosis"],["A15.8","Other respiratory tuberculosis"],["A15.9","Respiratory tuberculosis unspecified"]]
How would I extract the ICD code, and the diagnoses description?
So with what I get [["A15.0","Tuberculosis of lung"]
How would I make 2 strings out of it one for the A15.0 part, and another for the Tuberculosis of lung part.
I can tokenize it, but that would be quite a bit of work. There has to be an easier way.
Thanks in advance
In order to iterate on the elements of a JSON array, you have to cast the returned general object to
JSONArray.In your example
jsonArray.get(3)returns a general object because in a JSON you can have different types of objects such as number, string and array.However, you know that
jsonArray.get(3)returns an array so if you cast you can handle the result like an array or a list.This is an example that you can put after
System.out.println(jsonArray.get(3).toString());Output