1

I have a sample JSON file that I want to parse in Java using retrofit. I am new to retrofit and a bit new to Java as well. The examples I see on the web are not clear to me right now. Can somebody explain how I can use retrofit to extract the movie_logo field from the following JSON structure?

  "url":"sample_url",
  "movies_metadata":
  {
    "movies":
    {
       "Movie 1":
        {
          "Description":"Sample description for Movie 1",
           "Movie_Logo":"logo1.png"
        },
        "Movie 2":
        {
           "Description":"Sample description for Movie 2",
           "Movie_Logo":"logo1.png"
        },
       "Movie 3":
        {
           "Description":"Sample description for Movie 3",
           "Movie_Logo":"logo1.png"
        }
      }
   }
user2399453
  • 2,930
  • 5
  • 33
  • 60

1 Answers1

2

Retrofit isn't really used for parsing JSON into Java objects (internally it actually uses GSON for the parsing of API responses). I would suggest using JSON.org, GSON or Jackson for parsing your JSON file. The simplest way to do this is to use the JSON.org parser:

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;

@Slf4j
public class JsonTest {
    @Test
    public void test() throws Exception {
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet("http://jsonblob.com/api/jsonBlob/5384f843e4b0441b35d1329d");
        request.addHeader("accept", "application/json");
        HttpResponse response = client.execute(request);
        String json = IOUtils.toString(response.getEntity().getContent());

        //here's where you're actually parsing the JSON
        JSONObject object = new JSONObject(json);
        JSONObject metadata = object.getJSONObject("movies_metadata");
        JSONObject movies = metadata.getJSONObject("movies");
        JSONArray movieNames = movies.names();
        for (int i = 1; i< movieNames.length(); i++) {
            String movieKey = movieNames.getString(i);
            log.info("The current object's key is {}", movieKey);
            JSONObject movie = movies.getJSONObject(movieKey);
            log.info("The Description is {}", movie.getString("Description"));
            log.info("The Movie_Logo is {}", movie.getString("Movie_Logo"));
        }
    }
}

I put your JSON into a JSON Blob and then used their API to request it in the unit test. The output from the unit test is:

14:49:30.450 [main] INFO  JsonTest - The current object's key is Movie 2
14:49:30.452 [main] INFO  JsonTest - The Description is Sample description for Movie 2
14:49:30.452 [main] INFO  JsonTest - The Movie_Logo is logo1.png
14:49:30.452 [main] INFO  JsonTest - The current object's key is Movie 1
14:49:30.453 [main] INFO  JsonTest - The Description is Sample description for Movie 1
14:49:30.453 [main] INFO  JsonTest - The Movie_Logo is logo1.png
Tristan
  • 1,077
  • 11
  • 16