2015-10-29 198 views
0

嗨,大家好我有一个问题,解析我的嵌套json数组。这是我的示例JSON响应:Android解析嵌套json

{ 
    "SUCCESS": true, 
    "DATA": [ 
     { 
     "ShowData": [ 
      { 
      "ShowTitle": "Episode 1", 
      "Category": "Comedy" 
      }, 
      { 
      "ShowTitle": "Episode 1a", 
      "Category": "Drama" 
      }, 
      { 
      "ShowTitle": "Mr. Right", 
      "Category": "Musical" 
      }, 
      { 
      "ShowTitle": "The Making", 
      "Category": "Talk" 
      }, 
      { 
      "ShowTitle": "Presscon", 
      "Category": "Comedy" 
      }, 
      { 
      "ShowTitle": "Presscon 2", 
      "Category": "Drama" 
      }, 
      { 
      "ShowTitle": "Episode 2", 
      "Category": "Comedy" 
      }, 
      { 
      "ShowTitle": "Episode 2", 
      "Category": "Drama" 
      } 
     ] 
     } 
    ] 
    } 

这是我到目前为止已经试过:

活动:

ArrayList<HashMap<String, String>> showsList 
            = Parser.getShowsResponseBody(response); 

          ArrayList<HashMap<String, String>> result = new ArrayList<>(); 
          Set<String> titles = new HashSet<>(); 

          for(HashMap<String, String> map : showsList) { 
           if(titles.add(map.get("Category"))) { 
            result.add(map); 
           } 
          } 

分析器:

public static List<Show> getShowsResponseBody(Response response) { 
    BufferedReader reader = null; 
    StringBuilder sb = new StringBuilder(); 
    try { 
     reader = new BufferedReader(new InputStreamReader(response.getBody().in())); 
     String line; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    String result = sb.toString(); 

    List<WorldShow> list = new ArrayList<>(); 
    try { 
     JSONObject json = new JSONObject(result); 
     JSONArray jArray = json.getJSONArray("Data"); 
     for (int i = 0; i < jArray.length(); i++) { 
      JSONObject json_data = jArray.getJSONObject(i); 
      JSONArray arr = json_data.getJSONArray("ShowData"); 
      for(int j = 0; j < arr.length(); j++) { 
       JSONObject innerData = arr.getJSONObject(j); 

       Show show = new Show(); // Create Object here 

      show.setShowTitle(innerData.getString("ShowTitle")); 
      show.setCategory(innerData.getString("Category")); 
      list.add(show); // Finally adding the model to List 
      } 

     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    return list; 
} 

我的预期成果是:

Comedy: Episode 1, Presscon, Episode 2 
Drama: Episode 1a, Presscon 2, Episode 2 
Musical: Mr. Right 
Talk: The Making 

但是当我运行应用程序,它显示在所有类别中的所有记录。我的代码看起来有什么问题?我已经使用HashSet去除重复的对象,但它仍然是一样的。任何帮助将非常感激!提前致谢!

+0

你可能想要的东西像'的HashMap <字符串,收藏>'来存储多个显示每个类别。 – JimmyB

回答

1
// Make a map to hold the mapping between categories and shows: 
// (A single category is mapped to a collection of 1 or more shows) 
Map<String,List<Show>> catShows = new HashMap<String,List<Show>>(); 

// Put a Show object into the category map for its matching category: 
private void addShow(Map<String,List<Show>> map, Show show) { 

    // Get the shows already stored under that category: 
    List<Show> list = map.get(show.getCategory()); 

    if (list == null) { 
    // There's no entry for that category yet, so we create a new (empty) list: 
    list = new ArrayList<Show>(); 

    // Store the new list for its category: 
    map.put(show.getCategory(), list); 
    } 

    // Add the given show to the list for its category: 
    list.add(show); 

} 

// Example for how to iterate over the map created above: 
private void process(Map<String,List<Show>> map) { 

    for (Map.Entry<String, List<Show>> e : map.entrySet()) { 

    final String category = e.getKey(); 

    final List<Show> shows = e.getValue(); 
    // Now we have in shows the list of all shows for the category. 

    System.out.println("Cat: " + category); 

    // Output all shows for the current category:  
    for (Show s : shows) { 
     System.out.println (s.getShowTitle()); 
    } 

    } 
} 
+0

嗨,谢谢你,但我会把这个放在我的帕尔斯课堂上吗? –

+0

而且我无法在我的适配器中正确显示它 –

+0

您可以将它放入任何适合的类中。可能想为它创建一个专门的类。 – JimmyB

1

我想你可能会改变你的方法。

我建议你使用GSon Library并创建一个表示你的JSON类:

一个可能的场景:

Result.class

public class Result{ 
    boolean success; 
    List<Data> data; 

    //getter and setter 
} 

Data.class

public class Data{ 
List<Item> items;  

//getter and setter 
} 

Item.class

public class Item{ 
    String ShowTitle; 
    String Category; 

    //getter and setter 
} 

解析JSON:

Gson gson = new Gson(); 
Result result = gson.fromJson(json, Result.class); 

检查this

+0

不错的建议。然而,OP需要有点像'class Category {String name;集合显示; }',这在结构上与JSON不同,所以GSon&al。对此不会有帮助。 – JimmyB

+0

嗨,谢谢。但我不需要使用任何第三方库。 –