搜索ArrayList时的Java JSON ClassCastException
我的程序解析一个JSONArray,其中包含一个JSONOBEJCT列表,这些JSONOBEJCT是引导程序。然后,它迭代并创建一个飞行员的新实例(继承自Crew超类),并将其添加到名为loadedCrew的Crew类型的ArrayList中(它的Crew类型也将添加到列表中) 详情如下:搜索ArrayList时的Java JSON ClassCastException,java,arrays,json,classcastexception,Java,Arrays,Json,Classcastexception,我的程序解析一个JSONArray,其中包含一个JSONOBEJCT列表,这些JSONOBEJCT是引导程序。然后,它迭代并创建一个飞行员的新实例(继承自Crew超类),并将其添加到名为loadedCrew的Crew类型的ArrayList中(它的Crew类型也将添加到列表中) 详情如下: JSONObject crew = new JSONObject(json); //Create a new JSONObject called root from the json file sto
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);
}
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;
}
JSONObject crew=新的JSONObject(json)//从“json”中存储的json文件创建名为root的新JSONObject
JSONArray allPilots=crew.getJSONArray(“pilots”);
JSONArray allCabinCrew=crew.getJSONArray(“cabincrew”);
对于(int i=0;i
然后,当一个单独的方法在loadedCrew列表中找到符合条件的对象时,它应该返回一个名为pilotOutput的Pilot(list)类型的输出列表。
详情如下:
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);
}
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;
}
公共列表findPilotsByTypeRating(字符串类型代码){
//TODO自动生成的方法存根
pilotOutput.clear();
用于(机组a:装载机组)
{
如果(Pilot的实例&a.getTypeRatings()包含(typeCode));
{
pilotOutput.添加((Pilot)a);
}
}
返回先导输出;
}
但我得到了以下错误:
…引发异常:java.lang.ClassCastException:class baseclasses.CabinCrew无法强制转换为class baseclasses.Pilot
我无法理解为什么会发生这种情况,因为不仅对象已经是Pilot类型,而且当我将其分配到输出列表时,我甚至正在对其强制转换Pilot。我知道该方法没有试图向列表中添加任何cabinCrew,因为我使用了System.out.print(a)来确保它只获取引导对象
有什么想法吗?谢谢 删除
if
行末尾的分号。应该是这样的:
if (a instanceof Pilot && a.getTypeRatings().contains(typeCode))
pilotOutput.add((Pilot) a);
什么是PilotOut类型?这是解决方案!我已经花了好几个小时,甚至在添加分号时都没有考虑分号。非常感谢。