Java JSON到ArrayList<;CustomObject>;在忽略某些字段的情况下使用GSON
我希望使用GSON从JSON提要创建自定义对象的Java JSON到ArrayList<;CustomObject>;在忽略某些字段的情况下使用GSON,java,android,json,arraylist,gson,Java,Android,Json,Arraylist,Gson,我希望使用GSON从JSON提要创建自定义对象的ArrayList。我目前的方法可以很好地处理包含数组的单个JSON对象,但现在我需要解析更复杂的JSON对象。第一个JSON提要如下所示: {"data": {"item_id": "1", "element": "element1"} {"item_id": "2", "element": "element2"} {"item_id": "3", "element": "element3"} ... } {"
ArrayList
。我目前的方法可以很好地处理包含数组的单个JSON对象,但现在我需要解析更复杂的JSON对象。第一个JSON提要如下所示:
{"data":
{"item_id": "1", "element": "element1"}
{"item_id": "2", "element": "element2"}
{"item_id": "3", "element": "element3"}
...
}
{"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"
]}
我提取每个项目的方法是使用一个简单的自定义对象,并将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");
我在解析它时遇到了困难,因为数据类型混合,有些是数字、字符串,最后是数组。我构建了一个自定义对象,它接受另一个自定义对象的数组。这是集合对象:
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;
}
}
我并不真正关心获取其他元素(即“rate\u limit”、“api\u version”、“generated\u on”),我只想将“data”元素传递到对象的ArrayList
。但是,当我尝试与我的原始方法类似的方法时,解析器会停止对第一个对象的访问,因为它接收的是一个数字而不是一个数组。导致非法状态异常:应为BEGIN\u数组,但在第1行第17列path$处为数字。
。我想知道如何让解析器忽略其他元素,或者如何使用GSON分别获取每个元素
编辑:中针对我的问题提出的解决方案在技术上确实解决了我的问题。但这似乎是一个漫长的过程,对我来说是不必要的。我已经找到了一个简单得多的解决问题的方法,贴在我的答案下面。我也不确定这种方法是否能很好地解决上述问题,考虑到似乎没有一种方法可以通过GSON中的键名从
JsonArray
获取JsonObject
。我找到的解决方案是将数据解析成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();
InputStreamReader输入=新的InputStreamReader(connection.getInputStream());
JsonArray data=新建JsonParser().parse(输入).getAsJsonObject().getAsJsonArray(“数据”);
类型listType=newTypeToken(){}.getType();
ArrayList collection=new Gson().fromJson(数据,列表类型);
input.close();
此方法忽略所有其他JSON字段,仅获取指定的字段。它还从“数据”对象中获取对象数组,并将它们放入
CustomCollection
中自定义对象的ArrayList
中,该列表可能与use-volley库重复,而不是使用标准的网络请求。@RushiAyyappa这并不能直接解决我的问题,但它对我的应用程序有着不可估量的帮助。我甚至不知道Volley
库的存在,我觉得谷歌应该让它更加明显,特别是(在做了一些研究之后)AsyncTask
被认为是相当糟糕的。不管怎样,我最终还是使用了Square的改装
,我听说它比凌空
更快、更轻。但是谢谢你,这让我走上了一条更容易的道路。欢迎。快乐编码@布莱恩
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();