* @ projectName: AccountBooks * @ packageName: com.yyt.accountbooks.model * @ fileName: BillModel * @ author: zheng * @ createTime: 2023/1/11 17:32 * @ describe: 账本表对象 @Entity(tableName = "bills") public class BillModel { * balance : 0.0 * createTime : 1660880757000 * id : 2 * income : 0.0 * name : 账本一 * params : {} * pay : 0.0 * remark : 账本备注 * uid : 12 @PrimaryKey private int id; private double balance; private long createTime; private double income; private String name; private double pay; private String remark; private int uid; public double getBalance () { return balance; public void setBalance ( double balance) { this .balance = balance; public long getCreateTime () { return createTime; public void setCreateTime ( long createTime) { this .createTime = createTime; public int getId () { return id; public void setId ( int id) { this .id = id; public double getIncome () { return income; public void setIncome ( double income) { this .income = income; public String getName () { return name; public void setName (String name) { this .name = name; public double getPay () { return pay; public void setPay ( double pay) { this .pay = pay; public String getRemark () { return remark; public void setRemark (String remark) { this .remark = remark; public int getUid () { return uid; public void setUid ( int uid) { this .uid = uid; @Override public String toString () { return "BillModel{" + "balance=" + balance + ", createTime=" + createTime + ", id=" + id + ", income=" + income + ", name='" + name + '\'' + ", pay=" + pay + ", remark='" + remark + '\'' + ", uid=" + uid + '}' ;

Json数据

因为getData()的类型是Object的,获得的是data后面部份的数组,一共有13个数组对象。

string:{"code":0,"data":[{"balance":0.00,"createTime":1660880757000,"id":2,"income":0.00,"name":"账本一","params":{},"pay":0.00,"remark":"账本备注","uid":12},
{"balance":0.00,"createTime":1673076697000,"id":4,"income":0.00,"name":"pppppp","params":{},"pay":0.00,"remark":"pppppp","uid":12},
{"balance":0.00,"createTime":1673077667000,"id":5,"income":0.00,"name":"账本一","params":{},"pay":0.00,"remark":"账本一","uid":12},
{"balance":0.00,"createTime":1673077687000,"id":6,"income":0.00,"name":"账本一","params":{},"pay":0.00,"remark":"账本一","uid":12},
{"balance":0.00,"createTime":1673077696000,"id":7,"income":0.00,"name":"坎坎坷坷看看","params":{},"pay":0.00,"remark":"坎坎坷坷看看","uid":12},
{"balance":0.00,"createTime":1673079793000,"id":8,"income":0.00,"name":"账本一","params":{},"pay":0.00,"remark":"账本一","uid":12},
{"balance":0.00,"createTime":1673080112000,"id":9,"income":0.00,"name":"账本一","params":{},"pay":0.00,"remark":"账本一","uid":12},
{"balance":0.00,"createTime":1673080305000,"id":10,"income":0.00,"name":"哈哈哈哈哈那就","params":{},"pay":0.00,"remark":"哈哈哈哈哈那就","uid":12},
{"balance":0.00,"createTime":1673080319000,"id":11,"income":0.00,"name":"账本一","params":{},"pay":0.00,"remark":"账本一","uid":12},
{"balance":0.00,"createTime":1673080812000,"id":12,"income":0.00,"name":"账本一","params":{},"pay":0.00,"remark":"账本一","uid":12},
{"balance":0.00,"createTime":1673082156000,"id":13,"income":0.00,"name":"账本一","params":{},"pay":0.00,"remark":"账本一","uid":12},
{"balance":0.00,"createTime":1673086473000,"id":14,"income":0.00,"name":"用英语","params":{},"pay":0.00,"remark":"探讨探讨","uid":12},
{"balance":0.00,"createTime":1673088234000,"id":15,"income":0.00,"name":"滚滚滚","params":{},"pay":0.00,"remark":"","uid":12}]}

Gson库解析

错误思路1

直接把Object类型的Json数据直接反序列化为实体类的数组对象。

BillModel[] billModels =gson.fromJson((responseModel.getData().toString())
                                        ,BillModel[].class);

解析直接错误,解都解不出来.....

至于为什么?

应该是Object类型的数据直接toString不行。

错误思路2

把Object类型的Json数据先序列化为Json再反序列化为List<Object>对象。

目前的思路:用List把每个对象分出来,再用for循环遍历Gson库解析对象的每个属性。先把这data后面这部分toJson序列化为Json对象,在反序列化为一组一组的List<Object>对象。简单来说就是用Gson库序列化一次和反序列化各一次。

//第一次序列化和反序列化
List<Object> list= mGson.fromJson(mGson.toJson(responseModel.getData()),
                                    List.class);
//定义一个List
List<BillModel> billModelList=new ArrayList<>();
//遍历list,对每一项进行第二次反序列化
for(int i=0;i<list.size();i++){
    BillModel model=mGson.fromJson(list.get(i),BillModel.class);
    billModelList.add(model);
    //查看每一项的数据
    KLog.d(list.get(i)+"---------------------------");

我的思路结果本应该是这样的一组一组的。

{"balance":0.00,"createTime":1673076697000,
"id":4,"income":0.00,"name":"pppppp","params":{},
"pay":0.00,"remark":"pppppp","uid":12}

结果第二次反序列化打印数据的时候变成了下面这样一组一组!!!这样连Json格式都不是了,还怎么解析!!!

然后编译期间调用实体类的get方法可以,但是结果会出错甚至报错。

至于为什么?

应该是用List第一次解析为Obejct类型会把双引号给去掉,那么此时就不是Json格式了,就不能解析了。

{balance=0.0, createTime=1.660880757E12, id=2.0,
 income=0.0, name=小明, params={}, pay=0.0, remark=备注,
 uid=12.0}

 所以我知道这个办法行不通了,得换一种方法。

可以看看用List怎么反序列化的,在我写的下方链接博客里面第四点。

用TypeToken来储存实体类的类型。

blog.csdn.net/qFAFAF/arti…

List<BillModel> list= mGson.fromJson(mGson.toJson(responseModel.getData()),
                                    new TypeToken<List<BillModel>>(){}.getType());

和错误思路2的区别就是:

  • 我直接转化为BillModel实体类,而不是Object类型的了。
  • 先序列化一次,再进行一次反序列化即可,不需要第二次遍历每一项list进行反序列化。
  • if (BuildConfig.DEBUG) {
        for(int i=0;i<list.size();i++){
            KLog.d(list.get(i).getId()+"---------------------------");
    

    此时get()方法也能成功获取到数据了。

    而且一般整个app用一个Gson对象就行了,就只需要new一个Gson对象,放在全局变量去用就好了,避免资源浪费。