【发布时间】:2014-11-11 15:13:33
【问题描述】:
我有一个简单的 json 文件
{"name":"item1",
"category":"work",
"id":1},
{"name":"item2",
"category":"work",
"id":2},
{"name":"item3",
"category":"work",
"id":3}
{"name":"item4",
"category":"home",
"id":4}
转换为 ArrayList 我想按类别对项目进行分组,如下面的树视图
工作 -项目1 -项目2 -item3
首页 -item4
有什么想法吗?
private List<Item> item = new ArrayList<Item>();
try {
JSONObject responseObject = new JSONObject(response);
JSONObject itemsObject = responseObject.getJSONObject("items");
Iterator<?> keysIterator = itemsObject.keys();
while(keysIterator.hasNext()) {
String keyString = (String)keysIterator.next();
JSONObject coupon = couponsObject.getJSONObject(keyString);
Item item = new Item();
item.setName(item.getString("name"));
item.setCategory(item.getString("category"));
item.setID(item.getString("id"));
itemList.add(item);
}
} catch (Exception e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
【问题讨论】:
-
看起来你正在寻找
Map<String, List<Item>>。 -
如何将该 json 文件转换为 arraylist 是否有一个类来保存每个项目的数据?
-
检查上面我编辑了我的原始帖子
-
您可以简单地在
category上对数组列表进行排序,因此home项将出现在列表顶部,然后是work等
标签: java android json arraylist