Files
JS/02_advanced/uebungen/u24_obj-in-json/index.html
Philippe Torrel 199d580016
2026-07-08 11:55:05 +02:00

56 lines
1.5 KiB
HTML

<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Übung 24: Objekt in JSON umwandeln</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<main>
<div class="container py-5">
<div class="alert alert-primary">
<h2>Übung 24: Objekt in JSON umwandeln</h2>
<p>
Gegeben ist ein JavaScript-Objekt <code>book</code>. Konvertiere dieses Objekt in einen JSON-String und
parse anschließend die Zeichenkette zurück in ein JavaScript-Objekt. Gib den Titel und den Autor des Buches
in der Konsole aus.
</p>
<p>Folgendes sollte in der Konsole ausgegeben werden:</p>
<code>
<pre>
// JSON-String
{"title":"The Great Gatsby","author":"F. Scott Fitzgerald","year":1925}
// JavaScript Object
The Great Gatsby
F. Scott Fitzgerald
</pre>
</code>
</div>
</div>
</main>
<script>
'use strict';
const book = {
title: 'The Great Gatsby',
author: 'F. Scott Fitzgerald',
year: 1925,
};
// JSON String
const bookStr = JSON.stringify(book, '', 2);
console.log(bookStr);
// JS Object
const bookObj = JSON.parse(bookStr);
console.log(bookObj.title); // => 'The Great Gatsby'
console.log(bookObj.author); // => 'F. Scott Fitzgerald'
</script>
</body>
</html>