json Datei parsen

haiflosse

New member
Hallo!
Ich habe folgenden Code wo ich in data eine json Datei erhalte:

HTML:
<script>
const key = "1234";

const url = 'https://api.getgeoapi.com/v2/currency/convert?api_key=${key}&from=EUR&to=GBP&amount=10&format=json'

fetch(url)
  .then(response => response.json())
  .then(data => console.log(data));
</script>

Das Ergebnis sieht z.B. wie folgt aus:

Code:
{"base_currency_code":"EUR","base_currency_name":"Euro","amount":"10.0000","updated_date":"2022-06-14","rates":{"GBP":{"currency_name":"Pound sterling","rate":"0.8658","rate_for_amount":"8.6578"}},"status":"success"}

Ich möchte nun mit javascript nur den rate_for_amount in eine Variable speichern.
Hoffe es kann mir hier jemand weiterhelfen.
Vielen Dank
 
Ein bisschen ausführlicher:
HTML:
<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>Test</title>
	</head>

	<body>

		<input type="text" id="in">
		<input type="button" value="rate_for_amount Auslesen" onclick="konvert();">
		<div id="ergebnis"></div>

		<script>
			function konvert() {
				var data = JSON.parse(document.getElementById('in').value);
				document.getElementById('ergebnis').innerHTML = data.rates.GBP.rate_for_amount;
			}
		</script>
	</body>
</html>
 
Danke für die Antwort.
Hat super funktioniert.
Hier die Lösung:

HTML:
<script>
const key = "1234";

const url = 'https://api.getgeoapi.com/v2/currency/convert?api_key=${key}&from=EUR&to=GBP&amount=10&format=json'

fetch(url)
  .then(response => response.json())
  .then(data => konvert(data));
 

function konvert(data) {
				//var data = JSON.parse(document.getElementById('in').value);
				document.getElementById('ergebnis').innerHTML = data.rates.GBP.rate_for_amount;
			}
</script>
 
Zuletzt bearbeitet:
Zurück
Oben