现在的目的是这样的,我想从一串Object类型的字符串,转化成对应的实体类对象。
简单来说就是把这个字符串给一个个独立分开。
我先写出实体类对象和Json的数据,看完再开始解析。
实体类对象
package com.yyt.accountbooks.db.model; import androidx.room.Entity; import androidx.room.PrimaryKey; /** * @ 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不行。