Search code examples
androidjsonrotten-tomatoes

Trying to parse a JSON result from Rotten Tomatoes API


I'm succeeding in getting the movie's title and year, but somehow not the synopsis. as far as I understand the object's shape they should be reachable in the same way:

JSONObject resultOBJ = new JSONObject(result);
Log.v("hhhh",resultOBJ.toString());
JSONArray movArr = resultOBJ.getJSONArray("movies"); 
JSONObject movOBJ =movArr.getJSONObject(0);
String title = movOBJ.getString("title");
String synop = movOBJ.getString("synopsis");

nameView.setText(title);
synopView.setText(synop);

the object I receive looks like this (sans formatting):

{
    "total": 1,
    "movies": [
        {
            "critics_consensus": "Deftly blending comedy, ...",
            "id": "770672122",
            "mpaa_rating": "G",
            "ratings": {
                "audience_rating": "Upright",
                "audience_score": 87,
                "critics_rating": "Certified Fresh",
                "critics_score": 99
            },
            "release_dates": {
                "dvd": "2010-11-02",
                "theater": "2010-06-18"
            },
            "runtime": 103,
            "title": "Toy Story 3",
            "year": 2010,
            "synopsis": "Pixar returns to their first success with ...

et cetra...


Solution

  • I took your example (copy/paste), closed some gates for Json ond got proper result:

    public static void main(String[] args) throws JSONException  {
    
        String result = "{" + 
                "    \"total\": 1," + 
                "    \"movies\": [" + 
                "        {" + 
                "            \"id\": \"770672122\"," + 
                "            \"title\": \"Toy Story 3\"," + 
                "            \"year\": 2010," + 
                "            \"mpaa_rating\": \"G\"," + 
                "            \"runtime\": 103," + 
                "            \"critics_consensus\": \"Deftly blending comedy,adventure, and honest emotion, Toy Story 3 is a rare second sequel that really works.\"," + 
                "            \"release_dates\": {" + 
                "                \"theater\": \"2010-06-18\"," + 
                "                \"dvd\": \"2010-11-02\"" + 
                "            }," + 
                "            \"ratings\": {" + 
                "                \"critics_rating\": \"Certified Fresh\"," + 
                "                \"critics_score\": 99," + 
                "                \"audience_rating\": \"Upright\"," + 
                "                \"audience_score\": 87" + 
                "            }," + 
                "            \"synopsis\": \"Pixar returns to their first success with Toy Story 3. The movie begins with Andy leaving for college and donating his beloved toys -- including Woody (Tom Hanks) and Buzz (Tim Allen) -- to a daycare. While the crew meets new friends, including Ken (Michael Keaton), they soon grow to hate their new surroundings and plan an escape. The film was directed by Lee Unkrich from a script co-authored by Little Miss Sunshine scribe Michael Arndt. ~ Perry Seibert, Rovi\"" + 
                "        }" + 
                "    ]" + 
                "}";
    
    
        JSONObject resultOBJ = new JSONObject(result);
    
        JSONArray movArr = resultOBJ.getJSONArray("movies"); 
    
        JSONObject movOBJ =movArr.getJSONObject(0);
        String title = movOBJ.getString("title");
        String synop = movOBJ.getString("synopsis");
        System.out.println(synop); 
    }
    

    Output:

    Pixar returns to their first success with Toy Story 3. The movie begins with Andy leaving for college and donating his beloved toys -- including Woody (Tom Hanks) and Buzz (Tim Allen) -- to a daycare. While the crew meets new friends, including Ken (Michael Keaton), they soon grow to hate their new surroundings and plan an escape. The film was directed by Lee Unkrich from a script co-authored by Little Miss Sunshine scribe Michael Arndt. ~ Perry Seibert, Rovi