【问题标题】:Get specific index of JSON Nested objects in java Android在java Android中获取JSON嵌套对象的特定索引
【发布时间】:2021-10-25 05:31:14
【问题描述】:

如何仅获取“字段”数组下每个对象的“名称”字符串,主数组索引为 0,然后使用循环或其他超级想法获取下一个索引

[
    {
        "name": "Bank1",
        "fields": [
            {
                "name": "Email",
                "slug": "email",
                "type": "input"
            },
            {
                "name": "City",
                "slug": "city",
                "type": "input"
            },
            {
                "name": "Screenshot",
                "slug": "screenshot",
                "type": "file"
            },
            {
                "name": "Full Name",
                "slug": "full-name",
                "type": "input"
            }
        ],
        "status": "Active"
    },
    {
        "name": "Bank2",
        "fields": [
            {
                "name": "Email",
                "slug": "email",
                "type": "input"
            },
            {
                "name": "City",
                "slug": "city",
                "type": "input"
            },
            {
                "name": "Screenshot",
                "slug": "screenshot",
                "type": "file"
            },
            {
                "name": "Submitted Date",
                "slug": "submitted-date",
                "type": "calendar"
            }
        ],
        "status": "Active"
    }
]

我想要的输出:
电子邮件
城市
截图
全名

表示在输出中,我有索引 0,第一个对象数组数据...

我还做了什么

public void onResponse(String response) {
                        try {
                            JSONArray jsonArray = new JSONArray(response);


                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                               
                                String p_name = jsonObject.getString("name");

                                 ArrayList<String> arr = new ArrayList<>();

                                JSONArray ja = jsonObject.getJSONArray("fields");
                                int len = ja.length();

                                for (int j = 0; j < len; j++) {
                                    JSONObject json = ja.getJSONObject(j);
                                    arr.add(json.getString("name"));
                                }


}}catch block...

这给了我所有索引名称数据,我只想要特定的索引数据
我的当前输出:
电子邮件
城市
截图
全名
电子邮件
城市
截图
提交日期

【问题讨论】:

  • 使用 Hash map 存储索引及其值 name
  • 你怎么给我一个例子? @Piyush

标签: java android arrays json nested


【解决方案1】:

如果你想获取特定的索引数据,那么你应该在for循环中传递if条件

EX: 得到 index 0 的输出,例如,

电子邮件

城市

截图

全名

您的代码如下所示。

public void onResponse(String response) {
                    try {
                        JSONArray jsonArray = new JSONArray(response);


                        for (int i = 0; i < jsonArray.length(); i++) {
                          if(i==0){

                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                           
                            String p_name = jsonObject.getString("name");

                             ArrayList<String> arr = new ArrayList<>();

                            JSONArray ja = jsonObject.getJSONArray("fields");
                            int len = ja.length();

                            for (int j = 0; j < len; j++) {
                                JSONObject json = ja.getJSONObject(j);
                                arr.add(json.getString("name"));
                            }

                           }
 }}catch block...

【讨论】:

  • 但是会有无限索引 0,1,2,3.... 那我该怎么做呢?
  • 但我想你说你只想要特定的索引数据。(所以,我的意思是你的问题是当你添加到列表中时,你需要确定特定的索引)
  • 因此,您可能需要创建模型类。
  • 不,我只想要特定索引上的特定数据你有什么解决方案请帮助我
  • 您想获取所有数据,但另一方面您只需要特定的索引数据,对吗?
【解决方案2】:
               int i=gotIndex;//here you can give your exact index that you want
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                           
                            String p_name = jsonObject.getString("name");

                             ArrayList<String> arr = new ArrayList<>();

                            JSONArray ja = jsonObject.getJSONArray("fields");
                            int len = ja.length();

                            for (int j = 0; j < len; j++) {
                                JSONObject json = ja.getJSONObject(j);
                                arr.add(json.getString("name"));
                            }

在这里您使用了循环,您可以使用索引号 i (jsonObject = jsonArray.getJSONObject(i)) 在循环外使用 json 对象

【讨论】:

  • 但我想要下一个数组数据,这意味着 0 数据之后的索引 1 这就是为什么我使用循环任何解决方案? @AjayKS
  • 你真正的问题是什么?
  • 查看问题我想要特定的索引“字段”数组数据,在那个唯一的对象名称字符串下
  • 现在你会明白我已经更新了我的问题@AjayKS
  • 我觉得代码是对的,你的打印方式什么的不对,你说的逻辑是对的。实现方式不对。
猜你喜欢
  • 2021-10-24
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 2017-12-27
  • 2013-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多