转自:
http://blog.csdn.net/strive_for_tomorrow/article/details/53352509
在安卓开发过程中,常用的数据就使用的json格式数据。因为json格式的数据有:方便传输(减小多余字符,传输数据更少),
易于阅读及方便转换(Gson,FastJson等框架)等优势。
通常在开发中,要与服务器进行数据传输时,我们会和相关同事事先约定好数据格式,请求参数,返回json数据的字段等。
然后再根据返回json数据参数来定义接收数据的实体类,要求实体类中的属性字段与json返回的字段相对应。然后再解析数据,展示在我们的APP中。
有没有更简单地方法呢?比如我收到了服务端返回的json数据,直接用工具生成对应地实体类,不用我们一个字段一个字段地去对比来敲实体类的属性呢?
当然有,那就是AndroidStudio的插件GsonFormat。
使用方法:
第二步:在项目中新建一个实体类,比如我新建了测试类TestGsonBean,然后AndroidStudio工具中选择Code->Generate->GsonFormat,也可以使用快捷键Alt+S,如图:
第三步,转换数据
在第二步中,选择了GsonFormat后,会弹出一个数据转换的提示框,把我们要转换的json数据复制进去,点击OK键即可。当然你也可以通过Setting按钮对数据转换进行设置。如图:
点击OK键后,弹出数据格式设置提示框:
点击图右下角的OK键,对应地实体类就生成了:
public class TextGsonBean { /** * reason : 查询成功 * result : {"update":"2016-07-22 10:32:31","list":[["美元","100","665.63","660.3","668.3","666.69"],["港币","100","85.83","85.14","86.15","85.96"],["日元","100","6.2771","6.0834","6.3211","6.3014"],["欧元","100","732.74","710.13","737.88","735.79"],["英镑","100","879.28","852.15","885.46","879.01"]]} * error_code : 0 */ private String reason; private ResultEntity result; private int error_code; public void setReason(String reason) { this.reason = reason; } public void setResult(ResultEntity result) { this.result = result; } public void setError_code(int error_code) { this.error_code = error_code; } public String getReason() { return reason; } public ResultEntity getResult() { return result; } public int getError_code() { return error_code; } public static class ResultEntity { /** * update : 2016-07-22 10:32:31 * list : [["美元","100","665.63","660.3","668.3","666.69"],["港币","100","85.83","85.14","86.15","85.96"],["日元","100","6.2771","6.0834","6.3211","6.3014"],["欧元","100","732.74","710.13","737.88","735.79"],["英镑","100","879.28","852.15","885.46","879.01"]] */ private String update; private List> list; public void setUpdate(String update) { this.update = update; } public void setList(List
> list) { this.list = list; } public String getUpdate() { return update; } public List
> getList() { return list; } } }
是不是非常方便呢?赶紧试试吧,我使用的测试json数据是直接在网上找的数据,json数据内容如下: