2017-07-13 71 views
0

我正在使用巨型炸彈API製作web應用程序。如何從這樣的JSON中返回列表<Object>?

我被卡住了,因爲我需要從GET請求(使用Unirest)獲取搜索結果列表,然後將其方便地轉換爲名爲建議的對象列表。

這裏就是我的問題是:

private List<Suggestion> suggestions = new ArrayList<>(); 

public List<Suggestion> searchGames(){ 

    HttpResponse<String> request; 
    try { 
    request = Unirest.get("http://giantbomb.com/api/search/?api_key=xxxxxxxxxxxxxxxxxxxxxxxxx&format=json&resources=game&limit=10&field_list=name&query=creed") 
       .header("Accept", "application/json") 
       .asString(); 

    String requestString = request.toString(); //I can get my request as String, but that doesnt change much, I guess 

    //>>> the problematic part <<< 

    return suggestions; 
} 

我失去了一些東西,讓我到JSON響應從Unirest轉換到一個列表。下面是我的嘗試:

  • GSON - 問題JSON格式(有一個在JSON的,因爲有限制的領域開始一些數據,偏移等)
  • 對象映射器(Unirest /傑克遜) - 問題因爲我得到httpResponse而不是String/Object/JsonNode等
  • 使用JSONObject,JSONArray和for循環 - 也沒有運氣。

我從Unirest調用JSON看起來非常像這樣:

{ 
     "error": "OK", 
     "limit": 10, 
     "offset": 0, 
     "number_of_page_results": 1, 
     "number_of_total_results": 3, 
     "status_code": 1, 
     "results": [ 
      { 
       "name": "Assassin's Creed", 
       "resource_type": "game" 
      }, 
      { 
       "name": "The Creed", 
       "resource_type": "game" 
      }, 
      { 
       "name": "Assassin's Creed: Lost Legacy", 
       "resource_type": "game" 
      } 
     ] 
} 

我有遊戲類類似的解決方案,我顯示列表,名爲遊戲。

private List<Game> games = new ArrayList<>(); 

public List<Game> getAllGames(){ 
    return games; 
} 

我使用GET請求填充此列表,我輸入id和標題。

來自巨人炸彈API的建議對我來說比較棘手,因爲我使用外部API來獲取對象列表,而不是輸入我的輸入數據。

我Suggestion.java類看起來是這樣的:

package rest.library; 


public class Suggestion { 

    private long id; 
    private String name; 

    public Suggestion(){ 
    } 

    public Suggestion(long id, String name) { 
     this.id = id; 
     this.name = name; 
    } 

    public long getId() { 
     return id; 
    } 

    public String getName() { 
     return name; 
    } 

} 

在我的控制器我想這樣做:

@RequestMapping("/search") 
public List<Suggestion> searchGames() { return gameService.searchGames(); } 

下本地主機展望:8080 /搜索應該返回對象的列表,從JSON請求獲得的搜索結果。它應該看起來就像localhost:8080/games,它讓我回到我在Postman上使用GET發送的遊戲列表。我知道我從API中獲得了「resource_type」字段,並且沒有「id」字段,但是這對於以後來說是一個問題,因爲我認爲填充選定的類字段並增量添加id不會帶來太大的問題。

+0

https://stackoverflow.com/questions/17037340/converting-jsonarray-to-arraylist –

+0

這是一個ArrayList的(我越來越不兼容的錯誤類型),我不知道如何轉換請求(httpResponse <對象/ JsonNode /字符串>爲例如JSONArray。 – VapeKop

+0

字符串到JsonArray https://stackoverflow.com/questions/15609306/convert-string-to-json-array –

回答

1

你需要的是一種將你從請求中收到的String對象映射到HashMap中的方法,這樣你就可以設置你的對象屬性並形成一個返回列表(即使有像Jackson那樣的API)實施REST請求處理邏輯要少得多)。

這意味着首先使用response.getEntity().getContent()從HTTP請求中獲取InputStream,然後如here所述創建JSONParser。

這將使您能夠從JSON字符串獲取屬性,如here所示。

反過來,這將使您能夠使用setter方法創建您自己的對象列表。

讓我知道這是否有幫助。

+0

這很好,我會盡快研究這個。 傑克遜有多容易?我的意思是,我嘗試過,但也有麻煩。 – VapeKop

+0

我在我的工作場所使用傑克遜,一旦它成立,這很容易。您可以使用@GET批註,@Path(PATH_HERE)批註創建資源方法,並向方法中添加參數(任何類型),以表示方法正文和/或路徑接受的內容。這應該足以自動將JSON請求主體轉換爲指定的POJO。 –

+0

關於您的答案還有一個問題。爲什麼request.getEntity()。getContent()通知我getEntity()無法解析? – VapeKop

0

我建議使用建立在apache http api上的http-request。您可以創建類ResponseData來解析響應。

private static final HttpRequest<ResponseData> HTTP_REQUEST = 
     HttpRequestBuilder.createGet("http://giantbomb.com/api/search", ResponseData.class) 
     .addDefaultHeader("Accept", "application/json") 
     .addDefaultRequestParameter("api_key", "xxxxxxxxxxxxxxxxxxxxxxxxx") 
     .addDefaultRequestParameter("format", "json") 
     .addDefaultRequestParameter("resources", "game") 
     .addDefaultRequestParameter("limit", "10") 
     .addDefaultRequestParameter("field_list", "name") 
     .addDefaultRequestParameter("query", "creed") 
     .build(); 

    List<Suggestion> searchGames() { 
     ResponseHandler<ResponseData> responseHandler = HTTP_REQUEST.execute(); 
     ResponseData responseData = responseHandler.get(); 

     // Here create the List<Suggestion> from responseData 

     return suggestions 
    } 

private class ResponseData{ 
    private String error; 
    private int limit; 
    private int offset; 
    private int number_of_page_results; 
    private int number_of_total_results; 
    private int status_code; 
    private List<Result> results; 

    //getters and setters 
} 

private class Result{ 
    private String name; 
    private String resource_type; 

    //getters and setters 
} 
相關問題