2015-11-05 39 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中。