android - gson.fromjson 返回 null

标签 android json gson

我有这个来自 api 的 json。

{ "Table ":[[ "0 ", "--Please Select-- ",0],
            [ "L ", "APPLICANTS ",1],[ "N ", "CANDIDATES ",2],
            [ "C ", "CLIENTS ",3],[ "M ", "MEMBERS ",4],
            [ "S ", "STAFF ",5]], 
  "Table1 ":[[1, "BY EMAIL "], 
             [3, "BY PUSH "],
             [2, "BY SMS "],
             [4, "CheckIn "],
             [5, "CheckOut "]], 
  "Table2 ":[[ "ADMIN ", "ADMIN "],
             [ "CEO ", "CHIEF EXECUTIVE OFFICER "],
             [ "DON ", "DIRECTOR OF NURSING "],
             [ "FO ", "FINANCE OFFICER "],
             [ "GM ", "GENERAL MANAGER "],
             [ "MGR ", "MANAGER "],
             [ "OFA ", "OFFICER ALLOCATIONS "],
             [ "ORE ", "OFFICER RECRUITMENT "],
             [ "OTH ", "OTHERS "],
             [ "PO ", "PERSONAL ASSISTANT "],
             [ "REC ", "RECEPTIONIST "],
             [ "SO ", "SALES OFFICER "],
             [ "SW ", "SOCIAL WORKER "],
             [ "STAFF ", "STAFF "]], 
  "Table3 ":[], 
  "Table4 ":[[1, "CONTACT OUT "]], 
  "Table5 ":[[ "151504 ", "CONTACT MEMBER ", "LOGCONTMBR "],
             [ "151503 ", "EXTERNAL LOG ", "EXTRLOGONL "],
             [ "151501 ", "INTERNAL LOG ", "INTLOGONLY "],
             [ "151502 ", "SALES LOG ", "SALELOGONL "]], 
  "Table6 ":[], 
  "Table7 ":[], 
  "Table8 ":[[1, "New Client Visit "],
             [2, "Client Visit "]]}

这是我用来解析 json 的响应类。它是由 jsontopojo 插件生成的。

public class RecordDataModel{

@SerializedName("Table ")
@Expose
private List<List<String>> table = null;
@SerializedName("Table1 ")
@Expose
private List<List<Integer>> table1 = null;
@SerializedName("Table2 ")
@Expose
private List<List<String>> table2 = null;
@SerializedName("Table3 ")
@Expose
private List<Object> table3 = null;
@SerializedName("Table4 ")
@Expose
private List<List<Integer>> table4 = null;
@SerializedName("Table5 ")
@Expose
private List<List<String>> table5 = null;
@SerializedName("Table6 ")
@Expose
private List<Object> table6 = null;
@SerializedName("Table7 ")
@Expose
private List<Object> table7 = null;
@SerializedName("Table8 ")
@Expose
private List<List<Integer>> table8 = null;

public List<List<String>> getTable() {
    return table;
}

public void setTable(List<List<String>> table) {
    this.table = table;
}

public List<List<Integer>> getTable1() {
    return table1;
}

public void setTable1(List<List<Integer>> table1) {
    this.table1 = table1;
}

public List<List<String>> getTable2() {
    return table2;
}

public void setTable2(List<List<String>> table2) {
    this.table2 = table2;
}

public List<Object> getTable3() {
    return table3;
}

public void setTable3(List<Object> table3) {
    this.table3 = table3;
}

public List<List<Integer>> getTable4() {
    return table4;
}

public void setTable4(List<List<Integer>> table4) {
    this.table4 = table4;
}

public List<List<String>> getTable5() {
    return table5;
}

public void setTable5(List<List<String>> table5) {
    this.table5 = table5;
}

public List<Object> getTable6() {
    return table6;
}

public void setTable6(List<Object> table6) {
    this.table6 = table6;
}

public List<Object> getTable7() {
    return table7;
}

public void setTable7(List<Object> table7) {
    this.table7 = table7;
}

public List<List<Integer>> getTable8() {
    return table8;
}

public void setTable8(List<List<Integer>> table8) {
    this.table8 = table8;
}

}

这是我正在使用的 gson 代码。

RecordDataModel recordDataModel = new Gson().fromJson(reader,RecordDataModel.class);
                        System.out.println("size "+recordDataModel.getTable().get(0).get(0));

recordDataModel 抛出空指针异常,我知道格式有点生硬,但这是我所拥有的,我必须解析它。

我可能在这里遗漏了什么?

最佳答案

用下面的类替换你的 POJO

public class RecordDataModel {



    @SerializedName("Table ")
    @Expose
    private List<List<String>> table = null;
    @SerializedName("Table1 ")
    @Expose
    private List<List<String>> table1 = null;
    @SerializedName("Table2 ")
    @Expose
    private List<List<String>> table2 = null;
    @SerializedName("Table3 ")
    @Expose
    private List<String> table3 = null;
    @SerializedName("Table4 ")
    @Expose
    private List<List<String>> table4 = null;
    @SerializedName("Table5 ")
    @Expose
    private List<List<String>> table5 = null;
    @SerializedName("Table6 ")
    @Expose
    private List<String> table6 = null;
    @SerializedName("Table7 ")
    @Expose
    private List<String> table7 = null;
    @SerializedName("Table8 ")
    @Expose
    private List<List<String>> table8 = null;


    public List<List<String>> getTable() {
            return table;
    }

    public void setTable(List<List<String>> table) {
            this.table = table;
    }

    public List<List<String>> getTable1() {
            return table1;
    }

    public void setTable1(List<List<String>> table1) {
            this.table1 = table1;
    }

    public List<List<String>> getTable2() {
            return table2;
    }

    public void setTable2(List<List<String>> table2) {
            this.table2 = table2;
    }

    public List<String> getTable3() {
            return table3;
    }

    public void setTable3(List<String> table3) {
            this.table3 = table3;
    }

    public List<List<String>> getTable4() {
            return table4;
    }

    public void setTable4(List<List<String>> table4) {
            this.table4 = table4;
    }

    public List<List<String>> getTable5() {
            return table5;
    }

    public void setTable5(List<List<String>> table5) {
            this.table5 = table5;
    }

    public List<String> getTable6() {
            return table6;
    }

    public void setTable6(List<String> table6) {
            this.table6 = table6;
    }

    public List<String> getTable7() {
            return table7;
    }

    public void setTable7(List<String> table7) {
            this.table7 = table7;
    }

    public List<List<String>> getTable8() {
            return table8;
    }

    public void setTable8(List<List<String>> table8) {
            this.table8 = table8;
    }}

关于android - gson.fromjson 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48802640/

相关文章:

android - 如何获取视频的视频编解码器属性?

java - HTTP 状态 405 - 所请求的资源不允许指定的 HTTP 方法

java - GSON 已弃用。我们可以使用哪一个来序列化 Java 对象和 JSON 之间的反序列化?

ios - 如何在类似匹配 Json 的 Swift 中格式化日期?

java - Gson NumberFormatException

java - Gson反序列化接口(interface)到它的类实现

android - 如何在android中建立蓝牙连接?

android - SQLite UPDATE 值与行号可能/选项?

Android fragment 工厂方法与构造函数重载

Python:JSON字符串到字典列表 - 迭代时出错