WeInThis
WeInThis

Reputation: 547

Combine one Json with another Json (JAVA)

So I already succeed with one Json to get it all worked. Now I have another class where I want to get only one attribute. I have now a moviedatabase class (which work with JSON and gets all the information) and now I want to add a Trailer which is from Youtube API. so basically I need it to be added into the same JSON to make it easier for me in the future to get it into a HTML. the only problem is I cant get it work. I get a syntax error JSON when using this method.

EDIT CODE 1.1:

Youtube attribute:

public class FilmAttribut {
    private String title = "";
    private String release = "";
    private int vote = 0;
    private String overview = "";
    private String poster = "";
    private String trailer = "";

    // getters + setters stripped    
}

Youtube class:

public class Youtube {

    FilmAttribut movie = new FilmAttribut();
    public void search(String trailer, FilmAttribut in) {

        HttpResponse<JsonNode> response;
        try {
            response = Unirest.get("https://www.googleapis.com/youtube/v3/search?key=[app key here]&part=snippet")
                    .queryString("q", trailer + " trailer")
                    .asJson();

            JsonNode json = response.getBody();
            JSONObject envelope = json.getObject();
            JSONArray items = envelope.getJSONArray("items");

            in.setTrailer("https://youtu.be/" + items.getJSONObject(0).getJSONObject("id").getString("videoId")); //Gives me error here

        }
        catch (JSONException e) {

            e.printStackTrace(); 
        } catch (UnirestException e) {
            e.printStackTrace();
        }

        return null;

    }
}

and main method

public class WebService {

    public static void main(String[] args) {
        setPort(1337);

        Gson gson = new Gson();
        Youtube yt = new Youtube();
        MovieDataBase mdb = new MovieDataBase();




get("/search/:movie", (req, res) ->  {
            String titel = req.params(":movie");
            FilmAttribut film = mdb.searchMovie(titel); 
            yt.search(titel, film);
            String json = gson.toJson(film);
            return json;

        });

So I think the problem is that you can't have two gson.toJson(film) + gson.toJson(trailer); Because it makes the JSON twice, where one time is for the film (aka. movie) and then a new json is created with trailer which make the syntax error.

So my real question is, is it possible to have another class like I have now youtube. to send the information to a attribute class where I have all my attributes and then run it in main-method so that I can get all the JSON in one JSON.

Upvotes: 3

Views: 116

Answers (1)

user180100
user180100

Reputation:

If I did understand well what you are asking, yes you can, but I would do something like that instead:

public void search(String trailer, FileAttribut in) {
    // fetch the trailer from youtube (NB: you should use getters/setters, not public fields)
    in.setTrailer("https://youtu.be/" + items.getJSONObject(0).getJSONObject("id").getString("videoId"));
}

and:

FilmAttribut film = mdb.searchMovie(titel); 
yt.search(titel, film); // pass "film" to "fill" its trailer
return gson.toJson(film);

OR

public String search(String trailer) {
    // fetch the trailer from youtube 
    return "https://youtu.be/" + items.getJSONObject(0).getJSONObject("id").getString("videoId");
}

and:

FilmAttribut film = mdb.searchMovie(titel); 
film.setTrailer(yt.search(titel));
return gson.toJson(film);

Upvotes: 2

Related Questions