示例Json
{ "code": 0, "list": { "0": { "aid": "9698756", "author": "哔哩哔哩番剧", "copyright": "Copy", "create": "2017-04-08 18:39", "coins": 22043 }, "1": { "aid": "9854952", "author": "哔哩哔哩番剧", "copyright": "Copy", "create": "2017-04-15 15:29", "coins": 16991 } } }自定义JavaBean
public class FilmInfo { private int code; private List<FilmBean> list; @Override public String toString() { return "FilmInfo{" + "code=" + code + ", list=" + list + '}'; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public List<FilmBean> getList() { return list; } public void setList(List<FilmBean> list) { this.list = list; } public static class FilmBean{ private String aid; private String author; private int coins; private String copyright; private String create; @Override public String toString() { return "FilmBean{" + "aid='" + aid + '\'' + ", author='" + author + '\'' + ", coins=" + coins + ", copyright='" + copyright + '\'' + ", create='" + create + '\'' + '}'; } public String getAid() { return aid; } public void setAid(String aid) { this.aid = aid; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getCoins() { return coins; } public void setCoins(int coins) { this.coins = coins; } public String getCopyright() { return copyright; } public void setCopyright(String copyright) { this.copyright = copyright; } public String getCreate() { return create; } public void setCreate(String create) { this.create = create; } } }实现解析
private void specialJsonParse() { String json = "{\n" + " \"code\": 0,\n" + " \"list\": {\n" + " \"0\": {\n" + " \"aid\": \"9698756\",\n" + " \"author\": \"哔哩哔哩番剧\",\n" + " \"copyright\": \"Copy\",\n" + " \"create\": \"2017-04-08 18:39\",\n" + " \"coins\": 22043\n" + " },\n" + " \"1\": {\n" + " \"aid\": \"9854952\",\n" + " \"author\": \"哔哩哔哩番剧\",\n" + " \"copyright\": \"Copy\",\n" + " \"create\": \"2017-04-15 15:29\",\n" + " \"coins\": 16991\n" + " }\n" + " }\n" + "}"; FilmInfo filmInfo = new FilmInfo(); try { JSONObject jsonObject = new JSONObject(json); filmInfo.setCode(jsonObject.optInt("code")); List<FilmInfo.FilmBean> list = new ArrayList<>(); JSONObject object = jsonObject.optJSONObject("list"); for(int i = 0; i < object.length(); i++){ FilmInfo.FilmBean filmBean = new FilmInfo.FilmBean(); filmBean.setAid(object.getJSONObject(i+"").getString("aid")); filmBean.setAuthor(object.getJSONObject(i+"").getString("author")); filmBean.setCopyright(object.getJSONObject(i+"").getString("copyright")); filmBean.setCreate(object.getJSONObject(i+"").getString("create")); filmBean.setCoins(object.getJSONObject(i+"").getInt("coins")); list.add(filmBean); } filmInfo.setList(list); tv1.setText(json); tv2.setText(filmInfo.toString()); } catch (JSONException e) { e.printStackTrace(); } }Gson框架解析
将json对象字符串转换为javaBean
Api介绍(compile ‘com.google.code.gson:gson:2.8.0’)
fromJson(String json, Class<T> classOfT); //要求json对象中的key的名称与java对象对应的类中的属性名要相同json数据示例
{ "id":2, "name":"大虾", "price":12.3, "imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg" }实现解析
private void jsonToJavaBeanByGson() { String json = "{\n" + "\t\"id\":2, \"name\":\"大虾\", \n" + "\t\"price\":12.3, \n" + "\t\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"\n" + "}\n"; Gson gson = new Gson(); ShopBean shopBean = gson.fromJson(json, ShopBean.class); tv1.setText(json); tv2.setText(shopBean.toString()); }将json数组字符串转换成javaBean的List集合
Api介绍
fromJson(String json, Type typeOfT); //第二个参数传递Type类型 e.g. new TypeToken<List<ShopBean>>(){}.getType()