我在运行项目时遇到问题看到这个错误:" bookname":" farnaz"类型org.json.jsonobject无法转换为jsonarray。我搜索了这个错误,但没有找到这个问题。 这是我的代码:
public class Books {
long bookid;
String bookname;
String author;
String publisher;
double cost;
int likenumber;
int readedtimes;
public long getBookid() {
return bookid;
}
public void setBookid(long bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public int getLikenumber() {
return likenumber;
}
public void setLikenumber(int likenumber) {
this.likenumber = likenumber;
}
public int getReadedtimes() {
return readedtimes;
}
public void setReadedtimes(int readedtimes) {
this.readedtimes = readedtimes;
}
}
public class BooksJSONParser {
public static List<Books> parseFeed(String content) {
try {
JSONArray ar = new JSONArray(content);
List<Books> blist = new ArrayList<Books>();
for (int i = 0; i < ar.length(); i++) {
JSONObject obj = ar.getJSONObject(i);
Books b = new Books();
b.setBookname(obj.getString("bookname"));
b.setAuthor(obj.getString("author"));
b.setPublisher(obj.getString("publisher"));
b.setCost(obj.getDouble("cost"));`enter code here`
b.setBookid(obj.getLong("id"));
b.setLikenumber(obj.getInt("likes"));
b.setReadedtimes(obj.getInt("readedtimes"));`enter code here`
blist.add(b);
return blist;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}