1.定义对象,名字要与json对象一致
/** * totalCount : 28 * currentPage : 1 * totalPage : 28 * pageSize : 1 * list : [{"id":1,"name":"联想(Lenovo)拯救者14.0英寸游戏本(i7-4720HQ 4G 1T硬盘 GTX960M 2G独显 FHD IPS屏 背光键盘)黑","imgUrl":"http://7mno4h.com2.z0.glb.qiniucdn.com/s_recommend_55c1e8f7N4b99de71.jpg","description":null,"price":5979,"sale":8654}] */ private int totalCount; private int currentPage; private int totalPage; private int pageSize; /** * id : 1 * name : 联想(Lenovo)拯救者14.0英寸游戏本(i7-4720HQ 4G 1T硬盘 GTX960M 2G独显 FHD IPS屏 背光键盘)黑 * imgUrl : http://7mno4h.com2.z0.glb.qiniucdn.com/s_recommend_55c1e8f7N4b99de71.jpg * description : null * price : 5979.0 * sale : 8654 */ private List<ListBean> list; public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } public static class ListBean { private int id; private String name; private String imgUrl; private Object description; private double price; private int sale; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getImgUrl() { return imgUrl; } public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; } public Object getDescription() { return description; } public void setDescription(Object description) { this.description = description; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getSale() { return sale; } public void setSale(int sale) { this.sale = sale; } }
1.解析
//GSON直接解析成对象 ShopBean resultBean = new Gson().fromJson(strByJson,ShopBean.class); //对象中拿到集合 List<ShopBean.ListBean> userBeanList = resultBean.getList();