2016-06-13 177 views
-3

我有JSON这个样子,如何在ArrayList中设置ArrayList的值?

{ 
    "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]] 

任何人都可以给我suggetion吗?

+0

为什么使这个复杂的ArrayList > ??你为什么不用相同的课程? ArrayList ....这将是一个更好的方法 –

回答

0

你的基本的方法来解决问题是不对的,你可以很容易地与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<Category> category做到这一点。那么上面的问题就不会出现,代码会更清楚地被其他开发人员所理解。

+0

我也试过这个,它也替换了最后一个元素的所有位置 –

0

你的代码是潜在的越野车,你可以找到任何你在你的JSON希望以这样的方式

转到http://www.jsonschema2pojo.org/网站并粘贴您的JSON (这个网站生成Java类您的JSON(POJO))

复制java文件到您的项目

您应该使用序列化/反序列化库,供您JSON字符串转换成Java对象 我建议你GSON

进口GSON到您的项目写在这之后代码

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

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

+0

这实质上是一个仅链接的答案,因为如果链接子弹1死了这个答案是没用的。请将此答案修改为不使用非现场资源,因为只有链接的答案可能会被删除。 – Adriaan

0

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

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

您需要为您的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;} 

Topic.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' 
相关问题