add files

This commit is contained in:
2026-05-10 16:35:22 +03:30
parent f29dc564fb
commit 42f07c638d
14 changed files with 420 additions and 0 deletions
@@ -0,0 +1,38 @@
package org.FileExamples.ParsingJson;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
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());
JsonArray genresArray = rootObject.getAsJsonArray("genres");
Type type = new TypeToken<List<Genre>>(){}.getType();
List<Genre> genres = gson.fromJson(genresArray, type);
for(Genre genre : genres)
{
System.out.println(genre.getId() + ". " + genre.getName());
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}