Search code examples
androidjsonrotten-tomatoes

How do I access specfic JSON data from Rotten Tomatoes' API


I'm trying to develop a quick app that will return the upcoming movies and their abridged cast from this site: http://developer.rottentomatoes.com/docs/read/json/v10/Upcoming_Movies. However I am unsure on how to access the latest movies along with the abridged cast. I would greatly appreciate it if someone could help me out here as JSON is a little confusing for me.

I have used How to request movie data from Rotten Tomatoes using their JSON API? to help me thus far, but I want to be able to list the film and the cast in the format "The Hangover: Bradley Cooper, etc..." What I have so far is this

if (response != null)
{
    try
    {
        // convert the String response to a JSON object,
        // because JSON is the response format Rotten Tomatoes uses
        JSONObject jsonResponse = new JSONObject(response);

        // fetch the array of movies in the response
        JSONArray movies = jsonResponse.getJSONArray("movies");

        // add each movie's title to an array
        String[] movieTitles = new String[movies.length()];
        for (int i = 0; i < movies.length(); i++)
        {
            JSONObject movie = movies.getJSONObject(i);
            movieTitles[i] = movie.getString("title");
            JSONArray cast = movie.getJSONArray("abridged_cast");
            String[] castmembers = new String[cast.length()];
            for(int j =0; j<cast.length();j++){


            }
        }

        // update the UI
        refreshMoviesList(movieTitles);
    }
    catch (JSONException e)
    {
        Log.d("Test", "Failed to parse the JSON response!");
    }
}

Solution

  • If I understand your question you want your output format to be as follows:Matrix: Keane Reeves, Lawrence fishburne,.....
    Assuming that you are getting the right data from your json, the following will do:

       // add each movie's title to an array
        String[] movieTitles = new String[movies.length()];
        for (int i = 0; i < movies.length(); i++)
        {
            JSONObject movie = movies.getJSONObject(i);
            movieTitles[i] = movie.getString("title") + ":";
            JSONArray cast = movie.getJSONArray("abridged_cast");
            String[] castmembers = new String[cast.length()];
            for(int j =0; j<cast.length();j++){
                    movieTitles[i] += castmembers[j];
                    if( j < (cast.length()-1)){
                         movieTitles[i] += ",";
                     } 
            }
        }