我一直在使用 Xsrteam 将 XML 解析为 ArrayList。它有效,但由于某种原因我无法访问数组的内容。
处理 ArrayList 数据的类:
@XStreamAlias("BOARDS")
public class Boards {
@XStreamImplicit(itemFieldName = "id")
public ArrayList ids = new ArrayList();
//region GETTERS-SETTERS
public void setIds(ArrayList temp){
this.ids = temp;
}
public List getIds(){
return ids;
}
// endregion
}
@XStreamAlias("id")
class Id {
@XStreamAlias("board")
private String board;
@XStreamAlias("description")
private String description;
@XStreamAlias("price")
private String price;
@XStreamAlias("shape")
private String shape;
@XStreamAlias("riding_level")
private String ridingLevel;
@XStreamAlias("riding_style")
private String ridingStyle;
@XStreamAlias("camber_profile")
private String camber;
@XStreamAlias("stance")
private String stance;
@XStreamAlias("picture")
private String picture;
<<public getters - setters here>>
}
我如何尝试访问这些getter
:
Boards boards = (Boards) xstream.fromXML(reader); // parse xml into array list
boards.getIds().get(0).getPrice(); //!!getPrice() cannot be resolved
first
是 Object first =boards.getIds().get(0);
这是使用调试器的样子:
最佳答案
Boards
具有原始类型,因此不清楚 ArrayList ids
内应包含哪些对象类型。因此,您应该显式转换 boards.getIds().get(0)
的结果:
((Id) boards.getIds().get(0)).getPrice()
或泛化Boards
类:
public class Boards<E> {
public List<E> ids = new ArrayList<E>();
//region GETTERS-SETTERS
public void setIds(ArrayList<E> temp){
this.ids = temp;
}
public List<E> getIds(){
return ids;
}
// endregion
}
您可以阅读有关泛型的信息 here
关于java - 无法访问 ArrayList 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23758227/