2015-11-05 28 views
0

我期待從使用GSON的JSON訂閱源創建自定義對象的ArrayList。我目前的方法適用於一個單一的JSON對象持有一個數組,但現在我需要解析一個更復雜的JSON對象。第一JSON提要是這樣的:JSON到ArrayList <CustomObject>與GSON雖然忽略某些字段

{"data": 
    {"item_id": "1", "element": "element1"} 
    {"item_id": "2", "element": "element2"} 
    {"item_id": "3", "element": "element3"} 
    ... 
} 

我的方法,用於提取每個項目用一個簡單的自定義對象和解析JSON到這些對象的ArrayList

InputStreamReader input = new InputStreamReader(connection.getInputStream()); 

Type listType = new TypeToken<Map<String, ArrayList<CustomObject>>>(){}.getType(); 
Gson gson = new GsonBuilder().create(); 
Map<String, ArrayList<CustomObject>> tree = gson.fromJson(input, listType); 
ArrayList<CustomObject> = tree.get("data"); 

目前的JSON對象看起來是這樣的:

{"rate_limit": 1, "api_version": "1.2", "generated_on": "2015-11-05T19:34:06+00:00", "data": [ 
    {"collection": [ 
     {"item_id": "1", "time": "2015-11-05T14:40:55-05:00"}, 
     {"item_id": "2", "time": "2015-11-05T14:49:09-05:00"}, 
     {"item_id": "3", "time": "2015-11-05T14:51:55-05:00"} 
    ], "collection_id": "1"}, 
    {"collection": [ 
     {"item_id": "1", "time": "2015-11-05T14:52:01-05:00"}, 
     {"item_id": "2", "time": "2015-11-05T14:49:09-05:00"}, 
     {"item_id": "3", "time": "2015-11-05T14:51:55-05:00"} 
    ], "collection_id": "2" 
]} 

而且我無法解析它,因爲混合類型的數據,有些是數字,字符串,最後陣列。我有一個自定義對象,它構建了另一個自定義對象的數組。這是集合對象:

public class CustomCollection { 
    private String collection_id; 
    private ArrayList<CustomItem> collection_items = new ArrayList<>(); 

    public CustomCollection() { 
     this(null, null); 
    } 

    public CustomCollection(String id, ArrayList<CustomItem> items) { 
     collection_id = id; 
     collection_items = items; 
    } 

    public String getId() { 
     return collection_id; 
    } 

    public ArrayList<CustomItem> getItems() { 
     return collection_items; 
    } 
} 

這是項目目標:

public class CustomItem { 
    private String item_id; 
    private String item_element; 

    public CustomItem() { 
     this(null, null); 
    } 

    public CustomItem(String id, String element) { 
     item_id = id; 
     item_element = element; 
    } 

    public String getId() { 
     return item_id; 
    } 

    public String getElement() { 
     return item_element; 
    } 
} 

我真的不關心如何獲得其他元素(即「rate_limit」,「API_VERSION」,「generated_on」 ),我只想將「數據」元素傳遞給對象的一個​​ArrayList。但是當我嘗試類似於我的原始方法時,解析器會停止第一個對象,因爲它接收的是數字而不是數組。導致IllegalStateException: Expected BEGIN_ARRAY but was NUMBER at line 1 column 17 path $.。我想知道如何讓解析器忽略其他元素,或者如何使用GSON分別獲取每個元素。

編輯:我的問題的建議解決方案,在Ignore Fields When Parsing JSON to Object,發現在技術上確實解決了我的問題。但這似乎是一個漫長的過程,在我的情況下是不必要的。我發現我的問題更簡單的解決方案,發佈在我的答案下面。我也不確定這種方法是否適用於上述問題,因爲似乎沒有辦法通過GSON中的密鑰名稱從JsonArray獲得JsonObject

+0

[Gson - 將JSON解析爲對象時忽略json字段可能的重複](http://stackoverflow.com/questions/28423292/gson-ignore-json-fields-when-parsing-json-to-object) –

+1

使用排球庫而不是使用標準的網絡請求。 –

+0

@RushiAyyappa這並不能直接解決我的問題,但它有助於我的應用程序不可估量。我不知道「Volley」圖書館甚至存在,我覺得Google應該讓它更加明顯,尤其是因爲(在做了一些研究之後)「AsyncTask」被認爲是非常糟糕的。無論如何,我最終使用了Square的'RetroFit',我聽說它比'Volley'更快,更輕量。但是,謝謝你,這使我走上了一條更加輕鬆的道路。 – Bryan

回答

0

我發現的解決方案是將數據解析成com.google.gson.JsonObject,然後按鍵名稱分解爲JsonArray。然後,我可以使用此JsonArray作爲參數Gson.fromJson()將數據提取到自定義對象的我的ArrayList中。

InputStreamReader input = new InputStreamReader(connection.getInputStream()); 

JsonArray data = new JsonParser().parse(input).getAsJsonObject().getAsJsonArray("data"); 

Type listType = new TypeToken<ArrayList<CustomCollection>>(){}.getType(); 
ArrayList<CustomCollection> collection = new Gson().fromJson(data, listType); 

input.close(); 

此方法忽略所有其他JSON字段,只獲得指定的一個。它還從「數據」對象中獲取對象數組,並將它們放入CustomCollection內的自定義對象的ArrayList中。