I am doing a project using java. In that project I have to get the movie data from IMDB. So far I have learned that, using a direct link with movie id we can get the data as a JSON file.
http://www.omdbapi.com/?i=tt2975590&plot=full&r=json
I want to get this data to a JSON array in java. can someone help me with this. Thank you.
The download function that download the file & return the result :
private static String download(String urlStr) throws IOException {
URL url = new URL(urlStr);
String ret = "";
BufferedInputStream bis = new BufferedInputStream(url.openStream());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = bis.read(buffer, 0, 1024)) != -1) {
ret += new String(buffer, 0, count);
}
bis.close();
return ret;
}
Build JsonObject & convert to JsonArray like that :
try {
String ret = download("http://www.omdbapi.com/?i=tt2975590&plot=full&r=json");
if (ret != null) {
JSONObject items = new JSONObject(ret);
Iterator x = items.keys();
JSONArray jsonArray = new JSONArray();
while (x.hasNext()) {
String key = (String) x.next();
jsonArray.put(items.get(key));
System.out.println(key + " : " + items.get(key));
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}