这一节主要来说说我前一段时间遇到的一个JSON解析问题,开始我没解决,现在分析可能是当时习惯了写一个解析类,不知道会出现要写多个解析类的问题,没跳出现有的思维模式。最后还是让我以前的同桌帮我解决的,在这里表示感谢,现总结如下:
先来看一看要解析的东西:
{ "nav": [ { "hasmore": 0, "curpage": 1 }, { "id": 1005, "name": "熊出没", "method": 100, "isLeaf": 1 } ... ], "list": [ { "hasmore": 1, "curpage": 1 }, { "id": 10000010, "name": "猫和老鼠", "method": 15, "tracks": 142, "pic": "http://boscdn.shoujiduoduo.com/json-api/v1/duoduo-ring/pic%2F20000624.jpg", "score": "9.2", "restype": "duoduo" } ... ] }上面省略了一些相同的数据,用Gson解析,需要写解析类。解析类如下:
Root类:
public class Root { private List<Nav> nav ; private List<Item> list ; public void setNav(List<Nav> nav){ this.nav = nav; } public List<Nav> getNav(){ return this.nav; } public void setList(List<Item> list){ this.list = list; } public List<Item> getList(){ return this.list; } }Nav类:
public class Nav { private int hasmore; private int curpage; private int id; private String name; private int method; private String isLeaf; public int getHasmore() { return hasmore; } public void setHasmore(int hasmore) { this.hasmore = hasmore; } public int getCurpage() { return curpage; } public void setCurpage(int curpage) { this.curpage = curpage; } 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 int getMethod() { return method; } public void setMethod(int method) { this.method = method; } public String getIsLeaf() { return isLeaf; } public void setIsLeaf(String isLeaf) { this.isLeaf = isLeaf; } @Override public String toString() { return "Nav{" + "hasmore=" + hasmore + ", curpage=" + curpage + ", id=" + id + ", name='" + name + '\'' + ", method=" + method + ", isLeaf='" + isLeaf + '\'' + '}'; } }Item类
public class Item { private int hasmore; private int curpage; private String id; private String name; private int method; private int tracks; private String pic; private String score; private int hasseq; private String restype; public int getHasmore() { return hasmore; } public void setHasmore(int hasmore) { this.hasmore = hasmore; } public int getCurpage() { return curpage; } public void setCurpage(int curpage) { this.curpage = curpage; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMethod() { return method; } public void setMethod(int method) { this.method = method; } public int getTracks() { return tracks; } public void setTracks(int tracks) { this.tracks = tracks; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public int getHasseq() { return hasseq; } public void setHasseq(int hasseq) { this.hasseq = hasseq; } public String getRestype() { return restype; } public void setRestype(String restype) { this.restype = restype; } }
//json解析部分 Gson gson = new Gson(); Root bean = gson.fromJson(results,Root.class); watchSongInfosList.addAll(bean.getList()); adapter.notifyDataSetChanged();