我正在使用 JsonReader 从网站获取大量数据并保存到数据库。顺便说一句,只要读者没有找到对象项的值,它就会失败并停止执行。
这是我的错误;
System.err﹕ java.lang.IllegalStateException: Expected a string but was NULL at line 1 column 359337 path $[19].date
即使指定的值不可用,似乎在遇到错误之前加载的所有对象也会丢失并且不会保存到数据库中。有没有办法处理这些错误并保留到目前为止已加载的对象?我已将 JsonReader 设置为宽松。
这是解析数据的方法的一部分:
private Post read(JsonReader reader) throws Exception {
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
switch (name) {
case TITLE:
mTitle = new StringBuilder();
mTitle.append(reader.nextString());
break;
case AUTHOR:
if (reader.hasNext()) {
JsonToken peek = reader.peek();
if (peek == JsonToken.NULL) {
reader.skipValue();
} else {
reader.beginObject();
while (reader.hasNext()) {
String enclosedName = reader.nextName();
switch (enclosedName) {
case NAME:
mAuthor = new StringBuilder();
mAuthor.append(reader.nextString());
break;
case AVATAR:
mAvatar = new StringBuilder();
mAvatar.append(reader.nextString());
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
}
}
break;
case CONTENT:
mDescription = new StringBuilder();
mDescription.append(reader.nextString());
break;
case URL:
mEntryLink = new StringBuilder();
mEntryLink.append(reader.nextString());
break;
case DATE:
mDateStringBuilder = new StringBuilder();
mDateStringBuilder.append(reader.nextString());
break;
case GUID:
mGuid = new StringBuilder();
mGuid.append(reader.nextString());
break;
case FEATURED_IMAGE:
if (reader.hasNext()) {
JsonToken look = reader.peek();
if (look == JsonToken.NULL) {
reader.skipValue();
} else {
mFeaturedImage = new StringBuilder();
reader.beginObject();
while (reader.hasNext()) {
String itemToLookFor = reader.nextName();
switch (itemToLookFor) {
case SOURCE:
mFeaturedImage.append(reader.nextString());
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
}
}
break;
case TERMS:
if (reader.hasNext()) {
JsonToken check = reader.peek();
if (check == JsonToken.NULL) {
reader.skipValue();
} else {
reader.beginObject();
while (reader.hasNext()) {
String stuff = reader.nextName();
switch (stuff) {
case CATEGORIES:
reader.beginArray();
itemCategory = new StringBuilder();
while (reader.hasNext()) {
reader.beginObject();
while (reader.hasNext()) {
String item = reader.nextName();
switch (item) {
case NAME:
itemCategory.append(reader.nextString()).append(",");
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
}
reader.endArray();
break;
case TAGS:
reader.beginArray();
itemTags = new StringBuilder();
while (reader.hasNext()) {
reader.beginObject();
while (reader.hasNext()) {
String item = reader.nextName();
switch (item) {
case NAME:
itemTags.append(reader.nextString()).append(",");
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
}
reader.endArray();
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
}
}
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
return new Post(mAuthor.toString(), mAvatar.toString(), mDateStringBuilder.toString(), mTitle.toString(), mEntryLink.toString(), mDescription.toString(), mGuid.toString(), mFeaturedImage.toString(), itemTags.toString(), itemCategory.toString());
}