我是一个对JSON和GSON都很新的人。我的JSON结构如下所示,我正在使用gson库解析json对象并检索值,但是从getter中获取到了所有的空值。有人能帮帮我吗。
JSON文件:
患者JSON类:
JSON文件:
{
"Samples":[
{
"Id":"XX",
"SampleId":"XX",
"Gender":"XX"
}
]
}
Java-gson 代码:
BufferedReader br = new BufferedReader(new FileReader(/mnt/ftp/sample.json));
//convert the json string back to object
patientObj = gson.fromJson(br, PatientJson.class);
patient_id=patientObj.getId();
sample_id=patientObj.getSampleId();
gender=patientObj.getGender();
患者JSON类:
public class PatientJson {
String id,sampleId,gender;
//with all the three getter and setters.
}
id
,请确保 JSON 也是id
而不是Id
。 - Sarp Kaya