1

i try to use some web api, so i do this

public static void main(String[] args) {
    // Create Jersey client
    Client client = Client.create();

    // GET request to findBook resource with a query parameter
    String getSoccersSeasonsUrl = "http://api.football-data.org/v1/soccerseasons";
    WebResource webResourceGet = client.resource(getSoccersSeasonsUrl);
    webResourceGet.header("X-Auth-Token", myToken);
    ClientResponse response = webResourceGet.get(ClientResponse.class);
    String output = response.getEntity(String.class);

    System.out.println(output);
}

output

[{"_links":{"self":{"href":"http://api.football-data.org/v1/soccerseasons/394"},"teams":{"href":"http://api.football-data.org/v1/soccerseasons/394/teams"},"fixtures":{"href":"http://api.football-data.org/v1/soccerseasons/394/fixtures"},
"leagueTable":{"href":"http://api.football-data.org/v1/soccerseasons/394/leagueTable"}},
"id":394,
"caption":"1. Bundesliga 2015/16",
"league":"BL1",
"year":"2015",
"currentMatchday":24,
"numberOfMatchdays":34,
"numberOfTeams":18,
"numberOfGames":306,
"lastUpdated":"2016-03-01T20:50:44Z »}

how can i fill from this output directly in a java ArrayList of object like:

public class SoccerSeason {
    public SoccerSeason() {
    }
    private long id;
    private String caption;
    private String league;
    private String year;
    private long currentMatchday;
    private long numberOfMatchdays;
    private long numberOfTeams;
    private long numberOfGames;
    private String lastUpdated;
}

when i try to get directly SoccerSeason output = response.getEntity(SoccerSeason.class); i have a classic com.sun.jersey.api.client.ClientHandlerException what's missing in my code please? do you have any idea how to do this simply?

0

1 Answer 1

2

What you want is Google's GSON. It can be found with a quick google search, and it has a ton of easy to read documentation.

Add GSON to your projects dependencies/source code, add getters and setters for all of your class members to the class you've created and it should work beautifully.

It is used like this:

Gson gson = new Gson();
SoccerSeason newSoccerSeason = gson.fromJson(webApiResponse, SoccerSeason.class);
String lastUpdated = newSoccerSeason.getLastUpdated();

Where webApiResponse is a String representation of the JSON received as your web API's response. You can also define a class SoccerSeasonList which looks like this:

public class SoccerSeasonList {
    ArrayList<SoccerSeason> seasonList;

    // getters/setters
}

Of course, your incoming JSON would have to have an object called seasonList containing all of your SoccerSeason objects to match up with this definition.

But then, you could grab your list like so:

SoccerSeasonList seasonList = gson.fromJson(webApiResponse, SoccerSeasonList.class);
ArrayList<SoccerSeason> seasonArr = seasonList.getSeasonList();

And perform operations like so:

for(SoccerSeason ss : seasonArr)
    System.out.println(ss.getNumberOfMatchdays());

To recap: You simply match up your JSON object names and literals to their equivalent java types in a class, and call fromJSON on a String containing the JSON received from your web API that you'd like to parse, passing in the class you want the object parsed to.

Sign up to request clarification or add additional context in comments.

7 Comments

is my "output" a String representation of a JSON response?
i got this stack trace Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
@BaboucheMehdi So, I've actually run into that problem a bunch. Verify that your response is what it should be, this has happened to me when my web server returns an exception, and also make sure that all of your arrays are placed within an object, they have to have a key to be able to grab it via a function. If this is doesn't make sense, then think how would you actually grab that array from your parsed object? You have to say soccerSeason.getX(), but what is X? You need to have a way to get the array, so it needs to be placed in an object that has a name, so that Gson knows what to do.
@BaboucheMehdi You could either modify the response your web API provides server-side, or client-side. If you are worried about screwing up other operations involving this response, I would probably make the change client-side before your attempt to parse with Gson. On the other hand, if you plan to use Gson universally, make the change server-side.
Thank you @Riptyde your probabely right, because i tried several ways to parse response I will try to modifs à sting value, I don't have access to the server side 'is private api'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.