-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f57d142
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
out | ||
alura-stickers.iml |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class JsonParser { | ||
|
||
private static final Pattern REGEX_ITEMS = Pattern.compile(".*\\[(.+)\\].*"); | ||
private static final Pattern REGEX_ATRIBUTOS_JSON = Pattern.compile("\"(.+?)\":\"(.*?)\""); | ||
|
||
public List<Map<String, String>> parse(String json) { | ||
|
||
Matcher matcher = REGEX_ITEMS.matcher(json); | ||
if (!matcher.find()) { | ||
|
||
throw new IllegalArgumentException("Não encontrou items."); | ||
} | ||
|
||
String[] items = matcher.group(1).split("\\},\\{"); | ||
|
||
List<Map<String, String>> dados = new ArrayList<>(); | ||
|
||
for (String item : items) { | ||
|
||
Map<String, String> atributosItem = new HashMap<>(); | ||
|
||
Matcher matcherAtributosJson = REGEX_ATRIBUTOS_JSON.matcher(item); | ||
while (matcherAtributosJson.find()) { | ||
String atributo = matcherAtributosJson.group(1); | ||
String valor = matcherAtributosJson.group(2); | ||
atributosItem.put(atributo, valor); | ||
} | ||
|
||
dados.add(atributosItem); | ||
} | ||
|
||
return dados; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandlers; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
public static void main(String[] args) throws IOException, InterruptedException { | ||
|
||
// fazer a requisição HTTP com o metodo GET dos top 250 filmes de acordo com o site imdb. | ||
|
||
String url = "https://mocki.io/v1/9a7c1ca9-29b4-4eb3-8306-1adb9d159060"; | ||
URI endereco = URI.create(url); | ||
HttpClient httpClient = HttpClient.newHttpClient(); | ||
HttpRequest request = HttpRequest.newBuilder(endereco).GET().build(); | ||
HttpResponse<String> response = httpClient.send(request, BodyHandlers.ofString()); | ||
String body = response.body(); | ||
|
||
// extrair os dados que interessam (titulo, poster, classificação) | ||
|
||
JsonParser jsonParser = new JsonParser(); | ||
List<Map<String, String>> listaDeFilmes = jsonParser.parse(body); | ||
|
||
// manipular e exibir os dados | ||
|
||
for (Map<String, String> filme : listaDeFilmes) { | ||
System.out.println(filme.get("title")); | ||
System.out.println(filme.get("imDbRating")); | ||
System.out.println(filme.get("image")); | ||
System.out.println(); | ||
} | ||
|
||
|
||
} | ||
} |