【问题标题】:JSON To ArrayList<CustomObject> With GSON While Ignoring Certain FieldsJSON 到 ArrayList<CustomObject> 与 GSON,同时忽略某些字段
【发布时间】:2015-11-05 20:03:19
【问题描述】:

我希望使用 GSON 从 JSON 提要创建自定义对象的 ArrayList。我目前的方法适用于保存数组的单个 JSON 对象,但现在我需要解析更复杂的 JSON 对象。第一个 JSON 提要如下所示:

{"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”),我只想将“data”元素传递给对象的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

【问题讨论】:

  • 使用 volley 库而不是使用标准网络请求。
  • @RushiAyyappa 这并不能直接解决我的问题,但它对我的应用程序有不可估量的帮助。我不知道 Volley 库甚至存在,我觉得谷歌应该让它更明显,特别是因为(经过一些研究)AsyncTask 被认为非常糟糕。无论如何,我最终使用了 Square 的 RetroFit,我听说它比 Volley 更快、更轻量级。但是谢谢你,这让我走上了一条更轻松的道路。
  • 欢迎。快乐编码! @布莱恩

标签: java android json arraylist gson


【解决方案1】:

我找到的解决方案是将数据解析为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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多