Java JSON 搜索 ArrayList 时出现 ClassCastException
Java JSON ClassCastException when searching an ArrayList
我的程序解析一个包含作为飞行员的 JSONObejcts 列表的 JSONArray。然后它遍历并创建一个新的 Pilot 实例(继承自 Crew 超类)并将其添加到名为 loadedCrew 的 Crew 类型的 ArrayList(它是 crew 类型,因为机组人员也将被添加到列表中)。
如下:
JSONObject crew = new JSONObject(json); //Create a new JSONObject called root from the json file stored in 'json'
JSONArray allPilots = crew.getJSONArray("pilots");
JSONArray allCabinCrew = crew.getJSONArray("cabincrew");
for (int i = 0; i < allPilots.length(); i++) {
Pilot a = new Pilot();
JSONObject pilot = allPilots.getJSONObject(i);
JSONArray typeRatings = pilot.getJSONArray("typeRatings");
a.setForename(pilot.getString("forename"));
a.setSurname(pilot.getString("surname"));
a.setHomeBase(pilot.getString("homebase"));
a.setRank(Pilot.Rank.valueOf(pilot.getString("rank")));
for(int i1=0; i1 < typeRatings.length(); i1++)
{
a.setQualifiedFor(typeRatings.getString(i1));
}
loadedCrew.add(a);
}
当它在 loadedCrew 列表中找到符合条件的对象时,一个单独的方法应该 return 名为 pilotOutput 的 Pilot (List) 类型的输出列表。
如下:
public List<Pilot> findPilotsByTypeRating(String typeCode) {
// TODO Auto-generated method stub
pilotOutput.clear();
for(Crew a : loadedCrew)
{
if (a instanceof Pilot && a.getTypeRatings().contains(typeCode));
{
pilotOutput.add((Pilot)a);
}
}
return pilotOutput;
}
但我收到以下错误:
...threw an exception: java.lang.ClassCastException: class baseclasses.CabinCrew cannot be cast to class baseclasses.Pilot
我不明白为什么会这样,因为不仅对象已经是 Pilot 类型,而且我什至在将它分配给输出列表时将 Pilot 强制转换为它。我知道该方法不会尝试将任何 cabinCrew 添加到列表中,因为我已经使用 System.out.print(a) 来确保它只获取飞行员对象。
有什么想法吗?谢谢。
删除 if
行末尾的分号。应该是这样的:
if (a instanceof Pilot && a.getTypeRatings().contains(typeCode))
pilotOutput.add((Pilot) a);
我的程序解析一个包含作为飞行员的 JSONObejcts 列表的 JSONArray。然后它遍历并创建一个新的 Pilot 实例(继承自 Crew 超类)并将其添加到名为 loadedCrew 的 Crew 类型的 ArrayList(它是 crew 类型,因为机组人员也将被添加到列表中)。
如下:
JSONObject crew = new JSONObject(json); //Create a new JSONObject called root from the json file stored in 'json'
JSONArray allPilots = crew.getJSONArray("pilots");
JSONArray allCabinCrew = crew.getJSONArray("cabincrew");
for (int i = 0; i < allPilots.length(); i++) {
Pilot a = new Pilot();
JSONObject pilot = allPilots.getJSONObject(i);
JSONArray typeRatings = pilot.getJSONArray("typeRatings");
a.setForename(pilot.getString("forename"));
a.setSurname(pilot.getString("surname"));
a.setHomeBase(pilot.getString("homebase"));
a.setRank(Pilot.Rank.valueOf(pilot.getString("rank")));
for(int i1=0; i1 < typeRatings.length(); i1++)
{
a.setQualifiedFor(typeRatings.getString(i1));
}
loadedCrew.add(a);
}
当它在 loadedCrew 列表中找到符合条件的对象时,一个单独的方法应该 return 名为 pilotOutput 的 Pilot (List) 类型的输出列表。 如下:
public List<Pilot> findPilotsByTypeRating(String typeCode) {
// TODO Auto-generated method stub
pilotOutput.clear();
for(Crew a : loadedCrew)
{
if (a instanceof Pilot && a.getTypeRatings().contains(typeCode));
{
pilotOutput.add((Pilot)a);
}
}
return pilotOutput;
}
但我收到以下错误:
...threw an exception: java.lang.ClassCastException: class baseclasses.CabinCrew cannot be cast to class baseclasses.Pilot
我不明白为什么会这样,因为不仅对象已经是 Pilot 类型,而且我什至在将它分配给输出列表时将 Pilot 强制转换为它。我知道该方法不会尝试将任何 cabinCrew 添加到列表中,因为我已经使用 System.out.print(a) 来确保它只获取飞行员对象。
有什么想法吗?谢谢。
删除 if
行末尾的分号。应该是这样的:
if (a instanceof Pilot && a.getTypeRatings().contains(typeCode))
pilotOutput.add((Pilot) a);