Java Einzeiler: JSONvon URL einlesen mit org.apache.commons.io am Beispiel: Corona – COVID-19

Java Einzeiler: JSONvon URL einlesen mit org.apache.commons.io am Beispiel: Corona – COVID-19
Hatte hier schon mal über die JSON Lib

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

berichtet. Wie kann damit nun in einer Zeile eine URL abgefragt werden?

Mit Hilfe der org.apache.commons.io geht das ganz einfach. Also erst einmal die Lib in die pom.xml einbinden.

<dependency>
			<groupId>org.apache.directory.studio</groupId>
			<artifactId>org.apache.commons.io</artifactId>
			<version>2.4</version>
</dependency>

Was wollen wir nun beispielsweise abfragen? Alle reden von Corona. Also lesen wir die drei aktuellen Werte der Johns Hopkins University ein. Die werden über diese Webseite aktuell dargestellt. Hier mal ein Foto:
tinyurl.com/virus-coronaCorona Status

Die Daten werden auch über eine JSON-Rest API unter https://covid19.mathdro.id/api bereitgestellt. Hier mal das Ergebniss einer Abfrage:

Java Einzeiler: JSONvon URL einlesen mit org.apache.commons.io am Beispiel: Corona – COVID-19

So, dann brauchen wir nur noch in einer Java Zeile die Werte abfragen:


JSONObject json = new JSONObject(IOUtils.toString(new URL("https://covid19.mathdro.id/api"), Charset.forName("UTF-8")));

Hier das ganze Java-Beispiel:

package corona;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * COVID-19 global data as-a-service https://covid19.mathdro.id
 * 
 * Daten von: tinyurl.com/virus-corona
 * 
 * This website and its contents herein, including all data, mapping, and
 * analysis (“Website”), copyright 2020 Johns Hopkins University, all rights
 * reserved, is provided to the public strictly for educational and academic
 * research purposes.
 * 
 * Api: https://covid19.mathdro.id/api
 * 
 * https://github.com/mathdroid/covid19
 * 
 * License
 * 
 * MIT License 2020, mathdroid.
 * 
 * Transitively from the John Hopkins Site, the data may not be used for
 * commercial purposes.
 * 
 * @author Thomas Wenzlaff
 */
public class Corona {

	private static final String URL = "https://covid19.mathdro.id/api";

	public static void main(String[] args) throws JSONException, MalformedURLException, IOException {

		JSONObject json = new JSONObject(IOUtils.toString(new URL(URL), Charset.forName("UTF-8")));

		// System.out.println(json.toString());

		int bestaetigt = getWert(json, "confirmed");
		int erholt = getWert(json, "recovered");
		int gestorben = getWert(json, "deaths");
		String zuletztAktuallisiert = json.getString("lastUpdate");

		System.out.println("Anzahl bestätigte Personen: " + bestaetigt + ", genesen: " + erholt + ", gestorben: " + gestorben + ", zuletzt aktuallisiert: "
				+ zuletztAktuallisiert);
	}

	private static int getWert(JSONObject json, String key) {
		JSONObject bestaetigt = (JSONObject) json.get(key);
		int anzahl = bestaetigt.getInt("value");
		return anzahl;
	}
}

Und das Ergebnis des Programms als CSV:

Anzahl bestätigte Personen: 102469, genesen: 57436, gestorben: 3491, zuletzt aktuallisiert: 2020-03-07T10:43:02.000Z

Jeder neuer Programmlauf liefert die aktuellsten Werte. Und nicht vergessen …

Java Einzeiler: JSONvon URL einlesen mit org.apache.commons.io am Beispiel: Corona – COVID-19
Dank an Dr. Kleinhirn.eu für die kostenlose Bereitstellung beider Bilder 😉

Ähnliche Artikel:

  1. Wie kann eine Excel CSV Datei mit Java mit Apache commons eingelesen werden?
  2. Java Quickie: Die Kommandozeilen Argumente mit Apache Commons CLI bändigen
  3. Java to Json Beispiel mit Gson

wallpaper-1019588
Wings for Life World Run 2024
wallpaper-1019588
I Was Reincarnated as the 7th: Trailer enthüllt weitere Cast-Mitglieder + Visual
wallpaper-1019588
#1511 [Review] Manga ~ Rainbow Revolution
wallpaper-1019588
Kochen am Lagerfeuer – Rezepte