Android解析嵌套的json数组(一对多关系)

时间:2015-11-02 06:23:09

标签: java 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<Show> 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
        }

    }

}

我的预期输出是:

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

但是当我运行应用程序时,它会显示所有类别中的所有记录。我的代码似乎有什么问题?我已经使用HashSet删除重复的对象,但它仍然是相同的。任何帮助将不胜感激!提前谢谢!

更新

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder viewHolder;

    if(convertView == null) {

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.list_world_show_item, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.tvShowName = (TextView) convertView.findViewById(R.id.tvShowName);
        viewHolder.btViewAllShows = (MyButton) convertView.findViewById(R.id.btViewAllShows);
        viewHolder.rvWorldShows = (RecyclerView) convertView.findViewById(R.id.rvWorldShows);
        viewHolder.llBackground = (LinearLayout) convertView.findViewById(R.id.llBackground);
        viewHolder.showsManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        viewHolder.rvWorldShows.setLayoutManager(viewHolder.showsManager);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    if(position % 2 == 0) {
        viewHolder.llBackground.setBackgroundColor(context.getResources().getColor(R.color.main_blue_pressed));
    } else {
        viewHolder.llBackground.setBackgroundColor(context.getResources().getColor(R.color.main_blue));
    }

  viewHolder.tvShowName.setText(result.get(Constants.SHOW_CATEGORY));
    display(viewHolder.rvWorldShows, map, mList);
    return convertView;

}

3 个答案:

答案 0 :(得分:0)

以下代码更改:

  JSONObject json = new JSONObject(result);
    JSONArray jArray = json.getJSONArray("DATA");
    for (int i = 0; i < jArray.length(); i++) {
        JSONObject innerData = jArray .getJSONObject(i);
        JSONArray arr = json_data.getJSONArray("ShowData");
        for(int j = 0; j < arr.length(); j++) {
            JSONObject jbj= arr.getJSONObject(j);

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

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

快乐的编码。

答案 1 :(得分:0)

class MainClass{

    private boolean SUCESS;
    private List<MainClass> DATA;
    private List<SubClass> showData;

    private class SubClass{
        private String ShowTitle;
        private String Category
    }

}

使用DTO类,以便处理复杂的json对象非常容易。您可以使用单行解析json。

MainClass obj = new Gson().fromJson(result , MainClass.class);

答案 2 :(得分:0)

根据您的模型类层次结构的响应。

Class Response{
boolean Success;
 private List<DATA> dataList;
}

Class DATA{
 private List<ShowData> showDataList;
}

Class ShowDataList{
String ShowTitle;
String Category;
}