Android解析嵌套的json

时间:2015-10-29 11:41:36

标签: android json

大家好我解析我的嵌套json数组有问题。这是我的样本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删除了重复的对象,但它仍然是相同的。任何帮助将不胜感激!提前谢谢!

2 个答案:

答案 0 :(得分: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() );
    }

  }
}

答案 1 :(得分: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