0
respostas

Problema para mexer com json

public class TesteComApi2 {
    public static void main(String[] args) throws IOException, InterruptedException {
        HttpRequest request = HttpRequest.newBuilder()
        .GET()
        .uri(URI.create("https://imdb-api.com/en/API/Top250Movies/k_w7v2ikap"))
        .build();

        HttpClient cliente = HttpClient.newBuilder().build();

        HttpResponse<String> response = cliente.send(request, BodyHandlers.ofString());

        String json = response.body();

        String[] listas = parseJsonMovies(json);

        List<String> titles = parseTitles(listas);
        //titles.forEach(System.out::println);

        List<String> urlImages = parseUrlImages(listas);
        //urlImages.forEach(System.out::println);

        List<String> Notas = parseNotas(listas);
        //System.out.println(Notas);

        List<String> AnoDoFilme = parseYears(listas);
        //AnoDoFilme.forEach(System.out::println);

        Gson g1 = new Gson();

        Movies[] filmes = g1.fromJson(json, Movies[].class);
        List<Movies> m1 = new ArrayList<Movies>();
        m1 = Arrays.asList(filmes);



        for (Movies movies : filmes) {
            System.out.println(movies);
        }



    }
    private static String[] parseJsonMovies(String json){
        Matcher matcher = Pattern.compile(".*\\[(.*)\\].*").matcher(json);
        if(!matcher.matches()){
            throw new IllegalArgumentException("no match in " + json);
        }

        String [] moviesArray = matcher.group(1).split("\\},\\{");
        moviesArray[0] = moviesArray[0].substring(1);
        int last = moviesArray.length-1;
        String lastString = moviesArray[last];
        moviesArray[last] = lastString.substring(0, lastString.length() - 1);
        return moviesArray;
    }

    private static List<String> parseTitles(String[] moviesArray) {
        return parseAttribute(moviesArray, 2);
    }

    private static List<String> parseYears(String[] moviesArray) {
        return parseAttribute(moviesArray, 4);
    }

    private static List<String> parseUrlImages(String[] moviesArray) {
        return parseAttribute(moviesArray, 5);
    }
    private static List<String> parseNotas(String[] moviesArray) {
        return parseAttribute(moviesArray, 7);
    }

    private static List<String> parseAttribute(String[] moviesArray, int pos) {
        return Stream.of(moviesArray)
            .map(e -> e.split("\",\"")[pos]) 
            .map(e -> e.split(":\"")[1]) 
            .map(e -> e.replaceAll("\"", ""))
            .collect(Collectors.toList());
    }
}
package Model;

public record Movies(String id, String rank, String title,String fullTitle, String year, String image,String crew,String imDbRating,String imDbRatingCount) {

    public Movies(String id, String rank, String title,String fullTitle, String year, String image,String crew,String imDbRating,String imDbRatingCount) {
        this.id = id;
        this.rank = rank;
        this.title = title;
        this.fullTitle = title;
        this.year = year;
        this.image = image;
        this.crew = crew;
        this.imDbRating = imDbRating;
        this.imDbRatingCount = imDbRatingCount;
    }





}

da o erro Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $