【发布时间】:2020-05-14 10:20:22
【问题描述】:
我的清单有问题。我的目标是从我的 json 列表中创建一个 JTree。为此,我将我的 json 转换为要列出我的患者的位置。但我什至不能显示我的病人的名字。也许我的代码让我的问题更清楚。如有任何建议,我将不胜感激!
我的病人班:
public class Patient {
private String name;
private String surname;
private String pesel;
public Patient(String name, String surname, String pesel) {
this.name = name;
this.surname = surname;
this.pesel = pesel;
}
public String getName(){return name;}
public void setName(String name){this.name = name;}
public String getSurname(){return surname;}
public void setSurname(String surname){this.surname = surname;}
public String getPesel(){return pesel;}
public void setPesel(String pesel){this.pesel = pesel;}
}
我的病人名单: (此方法有效,当我使用它将药物 json 转换为药物列表时我没有问题)
public List<Patient> FromJsonToArray1() throws IOException {
String patientsJson = initArray("Patients.json").toString();
Gson gson = new Gson();
java.lang.reflect.Type patientsListType = new TypeToken<ArrayList<Patient>>() {}.getType();
List<Patient> patientArray = gson.fromJson(patientsJson, patientsListType);
return patientArray;
}
这是我显示患者姓名的功能。
public void jtreecreator() throws IOException {
List<Medicine> medicineList = FromJsonToArray();
List<Patient> patientList = FromJsonToArray1();
medicinesDataMethods medicinesdatamethods = new medicinesDataMethods();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Patients");
Patient c = patientList.get(0);
Medicine d = medicineList.get(0);
DefaultTreeModel treeModel = new DefaultTreeModel(root);
JTree tree = new JTree(treeModel);
JScrollPane scrollPane = new JScrollPane(tree);
JOptionPane.showMessageDialog(null,c.getSurname());
JOptionPane.showMessageDialog(null, d.getName());
}
调用此函数后显示 d.getName(),但 c.getSurname() 不起作用。
它是我存储患者的 json:
[
{
"Pesel": "1111",
"Surname": "Walker",
"Name": "Johny "
},
{
"Pesel": "11111",
"Surname": "Walker1",
"Name": "Johny1 "
}
]
调试后,我发现我在 FromJsonToArray1() 中创建的列表包含患者对象,但姓名、姓氏和 pesel 的值为空。 :C
【问题讨论】:
-
如果您调试
jtreecreator(),您是否在c和d对象中看到正确的值? -
我看到类似 models.medicine@123123
-
可以展开对象查看
name的值 -
怎么样?我没做?
-
您可以谷歌如何检查您正在使用的 IDE 的对象