我有一个写入文件的Jsonwriter。我想保存所有具有id,title,author,releaseate,type,description和link的Book对象。我仍然在releaseDate名称中得到0,但在数组中有正确的值。
JsonWriter writer = new JsonWriter(new OutputStreamWriter
(this.openFileOutput("books.json",Context.MODE_PRIVATE),"ISO-8859-2"));
writer.beginArray();
for(Book b : MainActivity.books){
writer.beginObject();
writer.name("id").value(b.getId());
writer.name("title").value(b.getTitle());
writer.name("author").value(b.getAuthor());
writer.name("releaseDate").value(b.getReleaseDate());
writer.name("type").value(b.getType());
writer.name("description").value(b.getDescription());
writer.name("link").value(b.getLink());
writer.endObject();
}
writer.endArray();
writer.close();
public class Book实现了Parcelable,Comparable {
public int id;
public String title;
public String author;
public String type;
public int releaseDate;
public String description;
public String link;
public int isfav;
public static final int OBTITLE = 1;
public static final int OBAUTHOR = 2;
public static final int OBRDATE = 3;
public static int orderby = OBTITLE;
public static int getOrderby() {
return orderby;
}
public static void setOrderby(int orderby) {
Book.orderby = orderby;
}
public Book(int id, String title, String author, String type, int releaseDate, String description, String link) {
this.id = id;
this.title = title;
this.author = author;
this.type = type;
this.releaseDate = releaseDate;
this.description = description;
this.link = link;
this.isfav = 0;
}
public Book(Parcel in) {
this.id = Integer.parseInt(in.readString());
this.title = in.readString();
this.author = in.readString();
this.type = in.readString();
this.releaseDate = Integer.parseInt(in.readString());
this.description = in.readString();
this.link = in.readString();
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getId() {
return id;
}
public int getIsfav() {
return isfav;
}
public String getType() {
return type;
}
public int getReleaseDate() {
return releaseDate;
}
public String getDescription() {
return description;
}
public String getLink() {
return link;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(String.valueOf(id));
dest.writeString(title);
dest.writeString(author);
dest.writeString(type);
dest.writeString(String.valueOf(releaseDate));
dest.writeString(description);
dest.writeString(link);
}
public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() {
public Book createFromParcel(Parcel in) {
return new Book(in);
}
public Book[] newArray(int size) {
return new Book[size];
}
};
@Override
public int compareTo(Book another) {
if(orderby == 2){
return this.getAuthor().compareTo(another.getAuthor());
} else if(orderby == 3){
return this.getReleaseDate() - another.getReleaseDate();
} else{
return this.getTitle().compareTo(another.getTitle());
}
}
}
public ArrayList<Book> beolvas(JsonReader reader) {
try {
ArrayList<Book> books = new ArrayList<Book>();
Book book;
String name;
reader.beginArray();
while (reader.hasNext()) {
reader.beginObject();
book = new Book(0, null, null, null, 0, null, null);
while (reader.hasNext()) {
name = reader.nextName();
if (name.equals("id")) {
book.id = reader.nextInt();
} else if (name.equals("title")) {
book.title = reader.nextString();
} else if (name.equals("author")) {
book.author = reader.nextString();
} else if (name.equals("releasedate")) {
book.releaseDate = Integer.parseInt(reader.nextString());
} else if (name.equals("type")) {
book.type = reader.nextString();
} else if (name.equals("description")) {
book.description = reader.nextString();
} else {
book.link = reader.nextString();
}
}
reader.endObject();
books.add(book);
}
reader.endArray();
reader.close();
return books;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
答案 0 :(得分:1)
更改为:
writer.name("releaseDate").value(Integer.toString(b.getReleaseDate()));
答案 1 :(得分:0)
releaseDate
类的字段Book
是int
的类型。请尝试使用String.valueOf()
包装它的值:
writer.name("releaseDate").value(String.valueOf(b.getReleaseDate()));
答案 2 :(得分:0)
我在写和读方法中错过了一点错误。 releaseate在write方法中有D,这就是读者从文件中读取0的原因。