Gson没有设置模型类

时间:2018-02-13 08:12:21

标签: android gson

我正在使用 gson 来设置我的模型类。

使用的依赖关系 - 编译' com.google.code.gson:gson:2.8.0 '

我的模特课:

public class DemoBean {

    private String curr_avl_bal;
    private String loan_acc_no;
    private String name;
    private String id;
    private String hasSubCode;

    public String getCurr_avl_bal() {
        return curr_avl_bal;
    }

    public void setCurr_avl_bal(String curr_avl_bal) {
        this.curr_avl_bal = curr_avl_bal;
    }

    public String getLoan_acc_no() {
        return loan_acc_no;
    }

    public void setLoan_acc_no(String loan_acc_no) {
        this.loan_acc_no = loan_acc_no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHasSubCode() {
        return hasSubCode;
    }

    public void setHasSubCode(String hasSubCode) {
        this.hasSubCode = hasSubCode;
    }
}

在我的活动中:

GsonBuilder mBuilder = new GsonBuilder(); 
Gson mGson = mBuilder.create(); 
DemoBean demoBean = mGson.fromJson(res,DemoBean.class);

但是demoBean为null。为什么会这样?

2 个答案:

答案 0 :(得分:0)

 YourObject parsed = new Gson().fromJson(jsons, YourObject.class);

试试这个

答案 1 :(得分:0)

我在下面的演示中测试了它的正常工作是我正在使用的代码

GsonBuilder mBuilder = new GsonBuilder();
        Gson mGson = mBuilder.create();
        String res = "{ \"curr_avl_bal\": \"562\", \"loan_acc_no\": \"841152\", \"name\": \"demo\", \"id\": \"545\", \"hasSubCode\": \"Y\" } ";
        DemoBean demoBean = mGson.fromJson(res, DemoBean.class);
        Logger.print("This demo bean" + demoBean.getCurr_avl_bal());