feat: complete all files

This commit is contained in:
2026-05-15 17:30:41 +03:30
parent 42f07c638d
commit 51cccccea4
9 changed files with 61 additions and 93 deletions
@@ -8,30 +8,29 @@ import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class ParsingJsonExample01 {
static void main() {
try
{
Path path = Path.of("data.json");
String jsonString = new String(Files.readAllBytes(path));
Gson gson = new Gson();
JsonObject rootObject = JsonParser.parseString(jsonString).getAsJsonObject();
JsonPrimitive jsonPrimitive = rootObject.getAsJsonPrimitive("total");
System.out.println(jsonPrimitive.getAsInt());
try {
Path path = Path.of("data.json");
String texts = new String(Files.readAllBytes(path));
JsonObject rootObject = JsonParser.parseString(texts).getAsJsonObject();
JsonArray genresArray = rootObject.getAsJsonArray("genres");
Gson gson = new Gson();
Type type = new TypeToken<List<Genre>>(){}.getType();
List<Genre> genres = gson.fromJson(genresArray, type);
for(Genre genre : genres)
for(Genre genre: genres)
{
System.out.println(genre.getId() + ". " + genre.getName());
System.out.println(genre.getId() + ". " + genre.getName());
}
}
catch (IOException e)
{
} catch (IOException e) {
throw new RuntimeException(e);
}
}