Android解析json嵌套 - Android parse nested json
- 此内容更新于:2015-10-30
主题:
嗨伙计们我有问题解析json数组嵌套。这是我的示例json响应:这是到目前为止我试过:活动:解析器:我的预期的输出是:但当我运行这个应用程序,它显示在所有类别的所有记录。似乎怎么了我的代码吗?我已经使用HashSet移除重复的对象,但是它仍然是相同的。任何帮助将很乐意欣赏!提前谢谢!
原文:
Hi guys I have a problem parsing my nested json array. This is my sample json response:
{ "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" } ] } ] }This is what I've tried so far:
Activity:
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); } }Parser:
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; }My expected output is:
Comedy: Episode 1, Presscon, Episode 2 Drama: Episode 1a, Presscon 2, Episode 2 Musical: Mr. Right Talk: The MakingBut when I run the app, it's displaying all the records in all category. What seem to be wrong with my code? I already used HashSet to remove duplicate objects but it's still the same. Any help would be gladly appreciated! Thanks in advance!
网友:你可能想要一个商店的数量显示为每个类别。
(原文:You probably want something like a HashMap<String,Collection<Show>> to store a number of shows for each category.)
解决方案:
原文:
// 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() ); } } }
解决方案:
我认为你可能会改变你的方法。我建议你使用GSon图书馆和创建一个类,代表你的json:可能的场景:结果。类数据。类项目。类解析json:检查这个
原文:
I think you might change your approach.
I suggest you to use GSon Library and create a class that represents your json:
Possible scenario:
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 }to parse json:
Gson gson = new Gson(); Result result = gson.fromJson(json, Result.class);check this
网友:不是一个坏的建议。然而,OP需要有点像,这从JSON结构不同,所以GSon&人不会真正帮助。