我在读取.JSON文件和创建DTO对象时遇到了麻烦……当我只读时,我从json文件中获得了第一项,其他所有内容都为null。 为什么会发生? 我的变量在类中的写入方式应与json中的相同?
Dto类
public class AlunoTestDto {
private String table;
private String rows;
private String AlunoID;
private String Nome;
private String RM;
private String RG;
private String usuario;
private String senha;
private String cursoAluno;
private String urlFoto;
private String email;
private String rgEscolar;
private String cpf;
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public String getRow() {
return rows;
}
public void setRow(String rows) {
this.rows = rows;
}//others
读取json的方法
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
AlunoTestDto alunoTestDto=null;
alunoTestDto = objectMapper.readValue(new File("alunos2.json"), AlunoTestDto.class);
Json文件
"table": "bws_aluno",
"rows":
[
{
"AlunoID": 1,
"Nome": "Mayk Reserv",
"RM": "4545454545",
"RG": "7535214-7",
"Usuario": null,
"Senha": null,
"CursoAluno": null,
"UrlFoto": null,
"Email": "",
"RGEscolar": null,
"CPF": null
},
{
"AlunoID": 2,
"Nome": "Philip Lombard",
"RM": "26585651",
"RG": "123456980",
"Usuario": null,
"Senha": null,
"CursoAluno": null,
"UrlFoto": null,
"Email": "",
"RGEscolar": null,
"CPF": null
},
{
"AlunoID": 3,
"Nome": "Bilbo Bolseiro",
"RM": "75898774",
"RG": "36522-8",
"Usuario": null,
"Senha": null,
"CursoAluno": null,
"UrlFoto": null,
"Email": "",
"RGEscolar": null,
"CPF": null
}
]
}
答案 0 :(得分:0)
"rows":
是数组或对象列表(比如说class Row
),
但是您的DTO使用row
作为字符串...
因此,JSON文件中没有任何内容可以填充"table"
元素