Avatar image for rayo329
#1 Edited by rayo329 (5 posts) -

I'm new working with API's and I'm making a tiny project with Java, I want to use a searchbar to search for a game title and get like, the first 5 results from this API. Then I want to click in the game I'm interested from those 5 results and get the information of that game.

I though, that it should be easy, I just have to request a /search/ and then when I click a game I should request a /game/ using the id I get from the search. But I'm having trouble with how this API works.

When I make a request to an API, I want to get that request in a JSON format and then turn that JSON into an object using a POJO class that contains the attributes shown in that JSON.

The problem is that no matter what I request to this API it just give me the same atributes: "error", "limit", "offset"... and then "results". And the information I want is in results. Now imagine I make a POJO class called "response" where I store those atributes, then I would have to create another POJO class called "results" to get the information I want. The main problem is that the "results" class needs some specific attributes if I request a game and completely different attributes if I request a Search.

So what I'm seeing is that if I want to use this api I can only request one thing, either a Search or a game, I must be wrong for sure, Have you tried to use this API with Java? What did you do? Am I asking a stupid question with an obvius answer? Can someone help me?

Avatar image for igrat
#2 Posted by igrat (31 posts) -

Presumably you just cast the results-object to whatever class corresponds with the API-call you made?

Avatar image for cuthbeorht
#3 Edited by cuthbeorht (1 posts) -

Hi @rayo329,

JSON is a portable format in a way that any language with an appropriate parser can serialize/deserialize it to/from objects; Java is no different.

Regarding Java specifically, you're going to have to choose a framework that does this for you (You could do it manually, but that's plain tedious ;) ). I can recommend Spring Boot since it is pretty easy to get started with. Spring Boot = Consume REST Api is a good place to start.

To answer your question specifically, you need to create POJO's (Data Transfer Objects - DTO's in this case) that maps to the JSON. The way I go about it is I take an example response and create my classes that way.

I hope this helps you get started.