Android:如何从可能更改对象名称的JSON响应中检索多个JSON对象?

pshaun:

我有用于过滤由一个列表视图以下JSON响应片断first_namelast_nameemail

"data": {

"filters": {
            "first_name": {
                "key": "first_name",
                "label": "First Name",
                "order": 1,
                "values": [
                    "Shaun",
                    "Max",
                    "Tyler"
                ],
                "filter_type": "select"
            },
            "last_name": {
                "key": "last_name",
                "label": "Last Name",
                "order": 2,
                "values": [
                    "Nash",
                    "Mally",
                    "Carick"
                ],
                "filter_type": "select"
            },
            "email": {
                "key": "email",
                "label": "Email",
                "order": 3,
                "values": [
                    "[email protected]",
                    "[email protected]",
                    "[email protected]"
                ],
                "filter_type": "select"
            }
        },

        ...

}

我的问题是first_namelast_nameemail是这意味着这些JSON对象名称可以更改自定义过滤器。例如,另一个响应可能如下所示:

"data": {

"filters": {
            "age": {
                "key": "age",
                "label": "Age",
                "order": 1,
                "values": [
                    "33",
                    "24",
                    "47"
                ],
                "filter_type": "select"
            },
            "middle_name": {
                "key": "middle_name",
                "label": "Middle Name",
                "order": 2,
                "values": [
                    "Nicholas",
                    "Ava",
                    "George"
                ],
                "filter_type": "select"
            },
            "email": {
                "key": "email",
                "label": "Email",
                "order": 3,
                "values": [
                    "[email protected]",
                    "[email protected]",
                    "[email protected]"
                ],
                "filter_type": "select"
            }
        },

        ...
}

在这里,所使用的自定义过滤器agemiddle_nameemail尽管这些过滤器的名称可能不同,每个滤波器总是有keylabelordervalues,和filter_type领域。

我无法理解如何使用GSON在此处正确解析信息。我尝试研究将JSON应用于POJO网站,但由于过滤器并不总是相同,因此我不知道如何应用它。我也尝试这样做:

JSONObject dataObject = myJSON.getJSONObject("data");
if(dataObject.has("filters")){
    JSONObject filterJSONObject = dataObject.getJSONObject("filters");
    //I need to retrieve the keys/values for each filter here
}

但这仅返回第一个过滤器对象,而不返回其余的过滤器对象。

任何帮助,将不胜感激

ror:

我建议使用其他方法。您可以创建这样的类(名称仅用于示例,但字段名称很重要):

public static class Holder {
        private final Filters data;

        public Holder(Filters data) {
            this.data = data;
        }

        public Filters getData() {
            return data;
        }
    }

    public static class Filters {
        private final Map<String, Value> filters;

        public Filters(Map<String, Value> filters) {
            this.filters = filters;
        }

        public Map<String, Value> getFilters() {
            return filters;
        }
    }

    public static class Value {
        private final String label;
        private final int order;

        public Value(String label, int order) {
            this.label = label;
            this.order = order;
        }

        public String getLabel() {
            return label;
        }

        public int getOrder() {
            return order;
        }
    }

上面的结构将序列化为示例json:

        Value value1 = new Value("label1", 1);
        Value value2 = new Value("label2", 2);
        Map<String, Value> data = new HashMap<>();
        data.put("age", value1);
        data.put("email", value2);
        Filters filters = new Filters(data);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        Log.d("json", gson.toJson(new Holder(filters)));

这是日志输出:

{
  "data": {
    "filters": {
      "age": {
        "label": "label1", 
        "order": 1
      }, 
      "email": {
        "label": "label2", 
        "order": 2
      }
    }
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何更改json对象名称(键)?

当对象名称中包含DOT时如何从json对象中检索字段值

使用if()更改对象名称

如何在 JSON 对象中更改对象的属性值

如何在C ++中更改JSON对象名称而不更改其值?

检索对象名称

如何从 JSON 对象中检索多个值

VBA以编程方式更改对象名称

从sequelize.js包含错误的对象名称中,从数据到数据的JSON响应中的方法

如何将 JavaScript 数组(不带对象名称)转换为 json(带对象名称)?

如何在Android中检索JSON对象

如何在 Django 中更改表对象名称?

如何使用 JSON.NET 将 JSON 数组中的对象名称用作属性值?

如何在 Django admin 中更改对象的名称?

如何在json上设置对象名称

当对象名称是数字时如何解析 JSON?

卡在Json API调用中的DateTime对象名称

如何通过对象名称动态检索对象属性?

Newtonsoft Json 解析不同的对象名称

JSON对象名称返回为ID

python:json打印根对象名称

在Android中查找MyPagerAdapter对象名称,如何?

在一个类中使用多个可能的对象名称反序列化JSON字符串

空手道:如果对象名称包含数组中的句点,如何设置Json值

Go中的JSON解码会更改对象类型吗?

如何在 PHP 中更改对象中 JSON 数组的输出?

如何使用RXJava / Android更改对象列表中的字段

如何使用 groovy 脚本比较 JSON 响应中的多个对象

在for循环中检索R中的Seurat对象名称