Java JSON decode from OMDB (IMDB) API using Java

Bilzard picture Bilzard · May 30, 2014 · Viewed 9k times · Source

I want to get movie data from the OMDB API which is JSON text. I am using Java to decode this and the package JSON-simple.

The URL I want to decode is this for example: http://www.omdbapi.com/?t=icarus

Outcome (directly copy and paste, not structured):

{"Title":"Icarus","Year":"2010","Rated":"TV-14","Released":"10 Dec 2010","Runtime":"42 min","Genre":"Adventure, Drama, Romance","Director":"Mairzee Almas","Writer":"Alfred Gough (creator), Jerry Siegel (character created by: Superman), Miles Millar (creator), Joe Shuster (character created by: Superman), Alfred Gough (developed for television by), Miles Millar (developed for television by), Genevieve Sparling","Actors":"Tom Welling, Erica Durance, Cassidy Freeman, Justin Hartley","Plot":"As the VRA threat intensifies, Clark takes initiative by closing down watchtower and declaring the League go officially underground, but will this be enough to stop trotter and Slade Wilson...","Language":"English","Country":"USA","Awards":"N/A","Poster":"http://ia.media-imdb.com/images/M/MV5BMjIwNDQ2MjM5OV5BMl5BanBnXkFtZTcwODU4NzU0NA@@._V1_SX300.jpg","Metascore":"N/A","imdbRating":"8.6","imdbVotes":"367","imdbID":"tt1628582","Type":"episode","Response":"True"}

The code under my button:

        String url = "http://www.omdbapi.com/?t=" + jListFilms.getSelectedValue().toString();

    try {
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(url);
        JSONObject jsonObj = (JSONObject) obj;

        String title = (String) jsonObj.get("Title") ;
        System.out.println(title);

    } catch (ParseException e){
        System.out.println(e);
    }

When I print out the variable title

Unexpected character (h) at position 0.

Does anyone know why I don't get the title of the movie?

Answer

Adam Yost picture Adam Yost · May 30, 2014

What your code is doing is parsing the string URL which starts "http://", hence the h at location 0.

You need to issue an HTTP GET request at that url in order to get the JSON back.

Here's an answer for how to issue the GET request