Я пытаюсь отфильтровать некоторые объекты из ответа JSON в Java. Ниже мой код. Мне нужно получить объект жанра из ответа и распечатать его отдельно. Кто-нибудь, кто знает, как это можно сделать?
Я сделал вызов RestAPI из omdb. Это простой проект, который я пытаюсь построить. Чтобы в основном проанализировать тип жанров, которые были выпущены в определенные годы.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import sun.org.mozilla.javascript.internal.json.JsonParser;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class OmdbApiService {
//public static final String Search_Url = "http://www.omdbapi.com/?s=TITLE&apikey=APIKEY";
//public static final String Search_Url = "http://www.omdbapi.com/?t=TITLE&plot=PLOT&apikey=APIKEY";
public static final String Search_Plot = "http://www.omdbapi.com/?i=TITLE&plot=PLOT&apikey=APIKEY";
private static String sendGetRequest(String requestURL){
StringBuffer response = new StringBuffer();
try {
URL url = new URL(requestURL);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
InputStream stream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
String line;
while ((line = buffer.readLine()) != null) {
response.append(line);
}
buffer.close();
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response.toString();
}
private static String searchMoviebyID(String title, String plot, String key) {
try {
title = URLEncoder.encode(title, "UTF-8"); // To omit the spaces in between the titles
plot = URLEncoder.encode(plot, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String requestUrl = Search_Plot
.replaceAll("TITLE", title)
.replaceAll("PLOT", plot)
.replaceAll("APIKEY", key);
return sendGetRequest(requestUrl);
}
/*private static String filterbyGenres(){
try {
}
}*/
public static void main(String[] args) {
String jsonResponse = OmdbApiService.searchMoviebyID("tt1345836","full","6d****87");
System.out.println(jsonResponse);
/*Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = new JsonParser().parse(jsonResponse);
System.out.println(gson.toJson(jsonResponse));*/
}
}
Выход:
{"Title":"The Dark Knight Rises","Year":"2012","Rated":"PG-13","Released":"20 Jul 2012","Runtime":"164 min","Genre":"Action, Thriller","Director":"Christopher Nolan","Writer":"Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)","Actors":"Christian Bale, Gary Oldman, Tom Hardy, Joseph Gordon-Levitt","Plot":"Despite his tarnished reputation after the events of The Dark Knight, in which he took the rap for Dent's crimes, Batman feels compelled to intervene to assist the city and its police force which is struggling to cope with Bane's plans to destroy the city.","Language":"English, Arabic","Country":"UK, USA","Awards":"Nominated for 1 BAFTA Film Award. Another 38 wins & 102 nominations.","Poster":"https://m.media-amazon.com/images/M/MV5BMTk4ODQzNDY3Ml5BMl5BanBnXkFtZTcwODA0NTM4Nw@@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"8.4/10"},{"Source":"Rotten Tomatoes","Value":"87%"},{"Source":"Metacritic","Value":"78/100"}],"Metascore":"78","imdbRating":"8.4","imdbVotes":"1,372,667","imdbID":"tt1345836","Type":"movie","DVD":"03 Dec 2012","BoxOffice":"$448,130,642","Production":"Warner Bros. Pictures","Website":"http://www.thedarkknightrises.com/","Response":"True"}
Это вывод, могу ли я узнать, как отфильтровать только жанр в этом выводе.
Дополнительная помощь: если кто-то может сказать мне, как напечатать вывод в отдельных строках, это будет полезно.