Ich versuche, 5 Stücke von JSON-Daten zu ziehen und sie in ein Excel-Blatt zu schreiben. Ich konvertiere die Daten in ein JSonarray und schreibe dann Excel mit POI. Aus irgendeinem Grund schreibt das Programm derzeit nur das fünfte Datenstück in die erste Zeile und tut nichts mit den anderen. Es sollte durch alle 5 Schleifen und setzen Sie jedes in die Zeile, die es entspricht. Irgendwelche Ideen?Schreiben Excel-Blatt mit Apache POI aus einem JSON-Array
private void sendPost() {
int rowNumber = 1;
while (rowNumber < 5) {
try {
String id = censusIdValue(rowNumber);
String url = "https://www.broadbandmap.gov/broadbandmap/analyze/jun2014/summary/population/censusplace/ids/" + URLEncoder.encode(id, "UTF-8") + "?format=json";
HttpClient client = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet);
InputStream is = response.getEntity().getContent();
String result = getStringFromInputStream(is);
JSONObject jsonObj = new JSONObject(result.toString());
JSONArray data = jsonObj.getJSONArray("Results");
int rowCount = 0;
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("wow");
JSONObject rec = data.getJSONObject(0);
String geographyId = rec.getString("geographyId");
String strStatusType = rec.getString("geographyName");
int numberOfWirelineProvidersEquals0 = rec.getInt("numberOfWirelineProvidersEquals0");
XSSFRow row = sheet.createRow(rowCount++);
XSSFCell cell1 = row.createCell(1);
cell1.setCellValue(geographyId);
XSSFCell cell2 = row.createCell(2);
cell2.setCellValue(strStatusType);
XSSFCell cell3 = row.createCell(3);
cell3.setCellValue(numberOfWirelineProvidersEquals0);
try (FileOutputStream outputStream = new FileOutputStream("data.xlsx")) {
workbook.write(outputStream);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
rowNumber++;
}
}
Sie sollten auf Ihrem jsonarray Schleife, um alle Zeilen in Ihrem Blatt zu schreiben. –