JSON

Android解析json嵌套 - Android parse nested json

字号+ 作者:H5之家 来源:H5之家 2015-11-02 19:31 我要评论( )

28im技术大牛是面向程序员的知识学习与分享社区,追求最前沿技术

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 Making

But 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&人不会真正帮助。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • php CI 实战教程:[5]用curl获取json并解析

    php CI 实战教程:[5]用curl获取json并解析

    2016-02-26 17:00

  •  JSON入门级学习总结-JSON数据结构

    JSON入门级学习总结-JSON数据结构

    2016-02-25 11:05

  • Android解析Json速度最快的库:json

    Android解析Json速度最快的库:json

    2016-02-13 18:00

  • JavaScript转换与解析JSON方法实例详解第1/2页

    JavaScript转换与解析JSON方法实例详解第1/2页

    2016-02-10 21:25

网友点评
j