嗨,我有一个 Json 字符串
{"lines":[{"line":"COLOMBO - BADULLA"},{"line":"COLOMBO - MATALE"},{"line":"COLOMBO - PUTTLAM"},{"line":"COLOMBO - THANDIKULAM"},{"line":"COLOMBO - TALAIMANNAR"},{"line":"COLOMBO - BATTICALOA"},{"line":"COLOMBO - TRINCOMALEE"},{"line":"COLOMBO - MATARA"},{"line":"COLOMBO - AVISSAWELLA"},{"line":"COLOMBO - MIHINTALE"}]}
及其域类为 This
public class Line{
private String line;
public String getLine(){
return this.line;
}
public void setLine(String line){
this.line = line;
}
}
public class Lines{
private List lines;
public List getLines(){
return this.lines;
}
public void setLines(List lines){
this.lines = lines;
}
}
并将gson映射到对象,如下所示
仅供参考:对于 jsonString,我传递的是字符串而不是 JSON URL(使用 HttpGet)
Line ln =null;
Gson gson = new Gson();
ln = gson.fromJson(jsonString, Line.class); not the URL
return ln;
抛出的异常是:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sab.loc/com.sab.loc.MainActivity}: java.lang.ClassCastException: com.sab.loc.Line cannot be cast to java.util.List
我不确定出了什么问题,我认为我使用http://jsongen.byingtondesign.com/生成的类定义是正确的
知道它在哪里做错了吗?