Files
LABS_EDUCATION/lr10/task_1/e4.java
2026-04-05 20:39:21 +05:00

33 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package lr10.task_1;
import java.io.FileReader;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class e4 {
public static void main(String[] args) {
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("example-json.json"));
JSONObject jsonObject = (JSONObject) obj;
System.out.println("Корневой элемент: "
+ jsonObject.keySet().iterator().next());
JSONArray jsonArray = (JSONArray) jsonObject.get("books");
for (Object o : jsonArray) {
JSONObject book = (JSONObject) o;
System.out.println("\nТекущий элемент: book");
System.out.println("Название книги: " + book.get("title"));
System.out.println("Автор: " + book.get("author"));
System.out.println("Год издания: " + book.get("year"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}