10

I can use Unirest to get an object of my own class like this:

HttpResponse<Item> itemResponse = Unirest.get("http://localhost:8080/item").asObject(Item.class);

I can also set the type parameter to List, which does give me a list of hash maps, but I'd like to get a list of items instead. Is this possible?

2 Answers2

27

Don't know if you're still waiting for an answer, but you should use an array. Like this;

HttpResponse<Item[]> itemResponse = Unirest.get("http://localhost:8080/item").asObject(Item[].class);
scuro
  • 828
  • 8
  • 12
  • 2
    Well this was for a class last semester, but I appreciate your help. I'm sure it will help someone else too! –  Jan 09 '16 at 00:40
4

Besides @scuro's answer, you can also get a list of objects from a response like this:

List<Item> items = Unirest.get("http://localhost:8080/item")
              .asObject(new GenericType<List<Item>>(){})
              .getBody();

From Unirest Docs.

i01573
  • 75
  • 2
  • 11
  • 1
    Just a note that `GenericType` is Java EE and won't be available in a Java SE project. In this case, @scuro's response works. – OOP Feb 17 '21 at 07:14