JSON

Android框架之路Json解析一篇就够(原生技术+Gson+FastJson)(2)

字号+ 作者:H5之家 来源:H5之家 2017-05-04 09:05 我要评论( )

示例Json {"code": 0,"list": {"0": {"aid": "9698756","author": "哔哩哔哩番剧","copyright": "Copy","create": "2017-04-08 18:39","coins": 22043},"1": {"aid": "9854952","author": "哔哩哔哩番剧","copyrigh

示例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()

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 其实算法面试就是技术界的应试考题

    其实算法面试就是技术界的应试考题

    2017-05-01 17:02

  • 浅谈 PHP 与手机 APP 开发(API 接口开发)

    浅谈 PHP 与手机 APP 开发(API 接口开发)

    2017-05-01 08:01

  • 学习ExtJS框架并完成ExtJS+SSH2+JSON实现树形结构(一)

    学习ExtJS框架并完成ExtJS+SSH2+JSON实现树形结构(一)

    2017-04-30 17:01

  • JSON技术在电力业务系统数据库监控工具中的应用

    JSON技术在电力业务系统数据库监控工具中的应用

    2017-04-28 11:00

网友点评