GsonFromat可以生成数据为JSON字符串的实体类。支持Gson、Jackson、FastJson解析。

首先安装插件
Android Studio GsonFormat插件的使用_第1张图片

Android Studio GsonFormat插件的使用_第2张图片

Android Studio GsonFormat插件的使用_第3张图片

举个列子,新建CourseForumGson类。
Android Studio GsonFormat插件的使用_第4张图片

Android Studio GsonFormat插件的使用_第5张图片

下面为刚才生成的实体:

package com.ntp.model.gson;import java.util.List;/** * 课程问题 * Created by lishuangxiang on 2015/12/16. */public class CourseForumGson {    /** * currentPage : 1 * forums : [{"content":"同一个静态(static)函数或变量的所有声明都必需包含static存储类型吗?","id":21,"replyNumber":4,"time":"2015-05-04T00:00:00","user":{"head":"http://192.168.0.105/ntp/upload/75110c11-c2db-42fe-8d1c-f7c1bfa3e3f1.jpg","name":"yanxing"}},{"content":"我似乎不能成功定义一个链表。我试过typedefstruct{char*item;NODEPTRnext;}*NODEPTR;但是编译器报了错误信息。难道在C语言中结构不能包含指向自己的指针吗?","id":22,"replyNumber":0,"time":"2015-05-04T00:00:00","user":{"head":"http://192.168.0.105/ntp/upload/768900e6-cfcc-434b-b5e3-f72077c67942.jpg","name":"time"}},{"content":"C语言难吗","id":23,"replyNumber":null,"time":"2015-05-04T00:00:00","user":{"head":"http://192.168.0.105/ntp/upload/768900e6-cfcc-434b-b5e3-f72077c67942.jpg","name":"time"}},{"content":"C语言难吗1","id":24,"replyNumber":null,"time":"2015-05-04T00:00:00","user":{"head":"http://192.168.0.105/ntp/upload/75110c11-c2db-42fe-8d1c-f7c1bfa3e3f1.jpg","name":"yanxing"}},{"content":"C语言难吗2","id":25,"replyNumber":null,"time":"2015-05-04T00:00:00","user":{"head":"http://192.168.0.105/ntp/upload/75110c11-c2db-42fe-8d1c-f7c1bfa3e3f1.jpg","name":"yanxing"}},{"content":"C语言难吗3","id":26,"replyNumber":null,"time":"2015-05-04T00:00:00","user":{"head":"http://192.168.0.105/ntp/upload/75110c11-c2db-42fe-8d1c-f7c1bfa3e3f1.jpg","name":"yanxing"}},{"content":"C语言难吗4","id":27,"replyNumber":null,"time":"2015-05-04T00:00:00","user":{"head":"http://192.168.0.105/ntp/upload/75110c11-c2db-42fe-8d1c-f7c1bfa3e3f1.jpg","name":"yanxing"}},{"content":"C语言难吗5","id":28,"replyNumber":null,"time":"2015-05-04T00:00:00","user":{"head":"http://192.168.0.105/ntp/upload/75110c11-c2db-42fe-8d1c-f7c1bfa3e3f1.jpg","name":"yanxing"}},{"content":"C语言难吗6","id":29,"replyNumber":null,"time":"2015-05-04T00:00:00","user":{"head":"http://192.168.0.105/ntp/upload/75110c11-c2db-42fe-8d1c-f7c1bfa3e3f1.jpg","name":"yanxing"}},{"content":"C语言难吗7","id":30,"replyNumber":null,"time":"2015-05-04T00:00:00","user":{"head":"http://192.168.0.105/ntp/upload/75110c11-c2db-42fe-8d1c-f7c1bfa3e3f1.jpg","name":"yanxing"}}] */    private int currentPage;    /** * content : 同一个静态(static)函数或变量的所有声明都必需包含static存储类型吗? * id : 21 * replyNumber : 4 * time : 2015-05-04T00:00:00 * user : {"head":"http://192.168.0.105/ntp/upload/75110c11-c2db-42fe-8d1c-f7c1bfa3e3f1.jpg","name":"yanxing"} */    private List<ForumsEntity> forums;    public void setCurrentPage(int currentPage) {        this.currentPage = currentPage;    }    public void setForums(List<ForumsEntity> forums) {        this.forums = forums;    }    public int getCurrentPage() {        return currentPage;    }    public List<ForumsEntity> getForums() {        return forums;    }    public static class ForumsEntity {        private String content;        private int id;        private int replyNumber;        private String time;        /** * head : http://192.168.0.105/ntp/upload/75110c11-c2db-42fe-8d1c-f7c1bfa3e3f1.jpg * name : yanxing */        private UserEntity user;        public void setContent(String content) {            this.content = content;        }        public void setId(int id) {            this.id = id;        }        public void setReplyNumber(int replyNumber) {            this.replyNumber = replyNumber;        }        public void setTime(String time) {            this.time = time;        }        public void setUser(UserEntity user) {            this.user = user;        }        public String getContent() {            return content;        }        public int getId() {            return id;        }        public int getReplyNumber() {            return replyNumber;        }        public String getTime() {            return time;        }        public UserEntity getUser() {            return user;        }        public static class UserEntity {            private String head;            private String name;            public void setHead(String head) {                this.head = head;            }            public void setName(String name) {                this.name = name;            }            public String getHead() {                return head;            }            public String getName() {                return name;            }        }    }}

本例中我用的是Gson解析json,解析代码如下:

/** * Created by lishuangxiang on 2015/12/18. */public class ParseJson {    public static <T> T convertJson(String json,Class<T> clazz){        Gson gson = new Gson();        T t = gson.fromJson(json,clazz);        return t;    }}

测试代码:

String json="";//json格式字符串CourseForumGson courseForumGson=ParseJson.convertJson(json, CourseForumGson.class);

Android Studio GsonFormat插件的使用_第6张图片

可见使用GsonFormat插件生成实体,对于复杂的json数据而言省时省力。

更多相关文章

  1. Android中屏幕密度和图片大小的关系分析
  2. 利用 nodejs 自动生成 Android 语言包实现应用内切换多语言的方
  3. android 动态向Gallery中添加图片及倒影&&3D效果
  4. Android图片堆叠效果实现
  5. android TextView和EditText中显示图片
  6. 工程 android上传图片至服务器
  7. Android 使用level-list改变对应的状态图片
  8. Android 反汇编Smali语言中插入log打印

随机推荐

  1. android打开,保存图片到sd卡,显示图片
  2. android String资源 包含 数学符号等特殊
  3. Android 自定义Combobox
  4. Android学习教程之日历控件使用(7)
  5. android 悬浮窗口的拖动
  6. Android Activity生命周期简明、详细介绍
  7. java.lang.NullPointerException at com.
  8. android 如何进入某个具体的应用管理页面
  9. Android(安卓)自定义标题栏背景
  10. ubuntu12.04 编译vlc for android 0.08