【问题标题】:How to set value in ArrayList inside ArrayList?如何在 ArrayList 内的 ArrayList 中设置值?
【发布时间】:2016-06-13 08:48:29
【问题描述】:

我有这样的 JSON,

{
  "resultFlag": true,
  "successMessage": "Data Received",
  "category": [
    {
      "CategoryID": "4",
      "CategoryTitle": "YSafety",
      "CategoryImage": "",
      "Percentage": "25%",
      "Level": 16,
      "Days": 60,
      "Topic": [
        {
          "TopicID": "11",
          "TopicTitle": "Safety during Online transactions",
          "TopicImage": ""
        },
        {
          "TopicID": "12",
          "TopicTitle": "Anti-Theft mobile Features",
          "TopicImage": ""
        },
        {
          "TopicID": "32",
          "TopicTitle": "Magic of Science",
          "TopicImage": ""
        }
      ],
      "Sponsor": [
        {
          "SponsorID": 1,
          "SponsorName": "adidas",
          "SponsorImage": "http://assets/upload/SPONSOR/adidas.jpg"
        }
      ]
    },
    {
      "CategoryID": "5",
      "CategoryTitle": "YSoccer",
      "CategoryImage": "",
      "Percentage": "25%",
      "Level": 16,
      "Days": 60,
      "Topic": [
        {
          "TopicID": "13",
          "TopicTitle": "Others",
          "TopicImage": ""
        },
        {
          "TopicID": "14",
          "TopicTitle": "Rules",
          "TopicImage": ""
        },
        {
          "TopicID": "33",
          "TopicTitle": "Magic of Nature",
          "TopicImage": ""
        }
      ],
      "Sponsor": [
        {
          "SponsorID": 1,
          "SponsorName": "adidas",
          "SponsorImage": "http://assets/upload/SPONSOR/adidas.jpg"
        }
      ]
    } ]
}

我的代码是在下面的数组中获取数组,

static ArrayList<String> al_sponsor_image = new ArrayList<String>();

    static ArrayList<ArrayList<String>> al_al_topic_id = new ArrayList<ArrayList<String>>();

    if (resultFlag == true) {
                            String success = response.getString("successMessage");

                            JSONArray array_category = response.getJSONArray("category");

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

                                JSONObject obj_category = array_category.getJSONObject(i);

                                String category_id = obj_category.getString("CategoryID");
                                String category_title = obj_category.getString("CategoryTitle");
                                String category_image = obj_category.getString("CategoryImage");
                                String percentage = obj_category.getString("Percentage");
                                String level = obj_category.getString("Level");
                                String days = obj_category.getString("Days");

                                //  al_category_id.add(category_id);
                                al_category_title.add(category_title);
                                //  al_category_image.add(category_image);
                                al_percentage.add(percentage);
                                al_level.add(level);
                                al_days.add(days);

                                JSONArray array_topic = obj_category.getJSONArray("Topic");

                                JSONArray array_sponsor = obj_category.getJSONArray("Sponsor");

                                al_topic_id.clear();
                                al_topic_title.clear();
                                al_topic_image.clear();

                                al_sponsor_id.clear();
                                al_sponsor_name.clear();
                                al_sponsor_image.clear();

                                for (int j = 0; j < array_topic.length(); j++) {

                                    JSONObject obj_topic = array_topic.getJSONObject(j);

                                    String topic_id = obj_topic.getString("TopicID");
                                    String topic_title = obj_topic.getString("TopicTitle");
                                    String topic_image = obj_topic.getString("TopicImage");

                                    al_topic_id.add(topic_id);
                                    al_topic_title.add(topic_title);
                                    al_topic_image.add(topic_image);

                                   /* String[] st_sub_array_topic_title = al_topic_title.toArray(new String[al_topic_title.size()]);
                                    String[] st_sub_array_topic_image = al_topic_image.toArray(new String[al_topic_image.size()]);

                                    st_array_topic_title = new String[i][];
                                    st_array_topic_title[j] = st_sub_array_topic_title;

                                    int[] itemImageList = new int[j];
                                    itemImageList[j] = R.drawable.dummy_flower;
                                    st_array_topic_image[j] = itemImageList;*/

                                    /*{itemImageList, itemImageList, itemImageList, itemImageList, itemImageList, itemImageList, itemImageList, itemImageList};*/
                                    /*{R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower};*/

                                    //  st_array_topic_image = new String[i][];
                                    //  st_array_topic_image[j] = st_sub_array_topic_image;

                                }

                                al_al_topic_id.add(i, al_topic_id);
                                al_al_topic_title.add(i, al_topic_title);
                                al_al_topic_image.add(i, al_topic_image);

                                System.out.println("al_al_topic_id : " + al_al_topic_id);
                                System.out.println("al_al_topic_title : " + al_al_topic_title);
                                System.out.println("al_al_topic_image : " + al_al_topic_image);

                                //     String[][] st_array_topic_image

                                for (int j = 0; j < array_sponsor.length(); j++) {

                                    JSONObject obj_sponsor = array_sponsor.getJSONObject(j);

                                    String sponsor_id = obj_sponsor.getString("SponsorID");
                                    String sponsor_name = obj_sponsor.getString("SponsorName");
                                    String sponsor_image = obj_sponsor.getString("SponsorImage");

                                    al_sponsor_id.add(sponsor_id);
                                    al_sponsor_name.add(sponsor_name);
                                    al_sponsor_image.add(sponsor_image);

                                    /*String[] st_sub_array_sponsor_name = al_sponsor_name.toArray(new String[al_sponsor_name.size()]);
                                    String[] st_sub_array_sponsor_image = al_sponsor_image.toArray(new String[al_sponsor_image.size()]);

                                    st_array_sponsor_name = new String[i][];
                                    st_array_sponsor_name[j] = st_sub_array_sponsor_name;

                                    st_array_sponsor_image = new String[i][];
                                    st_array_sponsor_image[j] = st_sub_array_sponsor_image;*/

                                }

                                al_al_sponsor_id.add(i, al_sponsor_id);
                                al_al_sponsor_name.add(i, al_sponsor_name);
                                al_al_sponsor_image.add(i, al_sponsor_image);

                                System.out.println("al_al_sponsor_id : " + al_al_sponsor_id);
                                System.out.println("al_al_sponsor_name : " + al_al_sponsor_name);
                                System.out.println("al_al_sponsor_image : " + al_al_sponsor_image);

                            }

                         /* String[] st_array_category_title = al_category_title.toArray(new String[al_category_title.size()]);
                            String[] st_array_percentage = al_percentage.toArray(new String[al_percentage.size()]);
                            String[] st_array_level = al_level.toArray(new String[al_level.size()]);
                            String[] st_array_days = al_days.toArray(new String[al_days.size()]); */

                            rv_learning_cards.setAdapter(new AdapterLearningCards(activity, al_category_title, al_percentage, al_level, al_days, al_al_topic_image, al_al_topic_title, al_al_sponsor_image));

                        }

当我打印 al_al_topic_title 时,它​​会将所有数组替换为 lat 数组...

al_al_topic_title : [[Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities]]

al_al_topic_title : [[Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others]]

谁能给我建议?

【问题讨论】:

  • 为什么要用 ArrayList> 让这件事变得复杂?你为什么不使用相同的类?什么是 ArrayList.... 这将是一个更好的方法

标签: android json arraylist


【解决方案1】:

您解决问题的基本方法是错误的,您可以使用具有的 bean 类 Category.java 轻松做到这一点

public class Category
{
   String CategoryID;
   String CategoryTitle;
   String CategoryImage;
   String Percentage;
   String Level;
   String Days;
   ArrayList<Topic> topic;

   .......
   Getter and setter methods here
   .........
}

class Topic
{
    String TopicID;
    String TopicTitle;
    String TopicImage;
     .......
   Getter and setter methods here
   .........

 }

现在将解析的数据添加到ArrayList&lt;Category&gt; category。那么上面的问题就不会出现,代码也会更清楚地被其他开发者理解。

【讨论】:

  • 我也试过这个,它也替换了所有位置的最后一个元素
【解决方案2】:

您的代码可能存在错误,您可以通过这种方式在 json 中找到您想要的任何内容:

1

转到http://www.jsonschema2pojo.org/ 站点并粘贴您的 json (此站点为您的 json (Pojo) 生成 java 类)

2

将 java 文件复制到您的项目中

3

您应该使用序列化/反序列化库将您的 json 字符串转换为 java 对象 我推荐你Gson

4

将 Gson 导入您的项目后,编写此代码

   Gson gson = new Gson();
   YOUR_GENERETED CLASS response = gson.fromJson(YOUR_JSON_STRING ,YOUR_GENERETED CLASS.class);

最后你可以从响应对象中获取你想要的任何东西

【讨论】:

  • 这本质上是一个仅链接的答案,因为如果项目符号 1 下的链接失效,这个答案就没有用了。请将此答案修改为不使用场外资源的答案,因为仅链接的答案可能会被删除。
【解决方案3】:

使用 Gson 解析您的 JSON 响应,而不是手动解析它。会减少很多问题。

Gson 是一个开源库,用于将 java 对象序列化和反序列化为 JSON。

您需要为您的 JSON 响应创建纯 java 对象类,如下所示。

CategoryJsonResponse.java

class CategoryJsonResponse {
boolean resultFlag;
String successMessage;
ArrayList<Category> category;}

Category.java

class Category {
int CategoryID;
String CategoryTitle;
String CategoryImage;
String Percentage;
int Level;
int Days;
ArrayList<Topic> Topic;
ArrayList<Sponsor> Sponsor;}

Sponser.java

class Sponsor {
int SponsorID;
String SponsorName;
String SponsorImage;}

主题.java

class Topic {
int TopicID;
String TopicTitle;
String TopicImage;}

现在您需要编写以下几行代码来解析您的 JSON 响应。我假设我们在“响应”变量中得到 JSON 响应。

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();

CategoryJsonResponse categoryJsonResponse = gson.fromJson(response.toString(), CategoryJsonResponse .class);

现在您在 categoryJsonResponse 对象中拥有完整的 JSON 响应,您可以用普通的 java 方式读取任何值。最后一点。要在您的项目中添加 GSON,您只需在您的应用级 gradle 文件中添加以下依赖项。

compile 'com.google.code.gson:gson:2.2.4'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 2013-07-10
    • 2012-03-28
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    相关资源
    最近更新 更多