嗨,我们使用带有 ORMLite 和 SpringAndroidSpiceRequest 的 RoboSpice 从网页读取 JSON 信息并写入 SQLite 数据库。
我们正在使用最新的 JSON 映射器 MappingJackson2HttpMessageConverter。
// Web services support JSON responses
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
final List<HttpMessageConverter<?>> listHttpMessageConverters = restTemplate.getMessageConverters();
我们已经尝试像下面的例子那样读取嵌套的 JSON 查询,我们可以读取并存储到数据库,JSON 的第一级(页面,总数),但我们不能存储 JSON 数组的下一层,所有的“schedule_date "条目。
我们已经将这个示例(以及更多示例)视为存储 JSON 数据的最佳示例。但这对我们不起作用。RoboSpice 使用 OrmLite 持久化 JSON 数组
问题:我们如何读取和写入嵌套的 JSON 键和值,我们很难过!:O
JSON:
{
"page": 2,
"total": 5,
"result":
[
{
"schedule_date": "2013-08-03"
},
{
"schedule_date": "2013-09-03"
},
{
"schedule_date": "2013-10-03"
},
{
"schedule_date": "2013-11-03"
},
{
"schedule_date": "2013-12-03"
}
]
}
广播结果.java:
import java.util.Collection;
import java.util.List;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.j256.ormlite.field.ForeignCollectionField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
@DatabaseTable(tableName="BroadcastResult")
public class BroadcastResult {
@DatabaseField(id = true)
private int page;
@DatabaseField
private int total;
@ForeignCollectionField(eager = false)
private Collection<Broadcast> result;
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public Collection<Broadcast> getResult() {
return result;
}
public void setResult(Collection<Broadcast> result) {
this.result = result;
}
}
广播.java:
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
@DatabaseTable(tableName="Broadcast")
public class Broadcast {
@DatabaseField(id = true)
private String schedule_date;
@DatabaseField(foreign = true)
private BroadcastResult result;
public String getScheduleDate() {
return schedule_date;
}
public void setScheduleDate(String schedule_date) {
this.schedule_date = schedule_date;
}
public void setBroadcastResult(BroadcastResult result) {
this.result = result;
}
public BroadcastResult getBroadcastResult() {
return result;
}
}
我们得到了这个错误,我们不明白。
Logcat:SQLiteLog(10637):(1)没有这样的列:result_id
@DatabaseField(foreign = true) // also tried (foreign = true, ColumnName="result")
private BroadcastResult result;
运行测试的完整日志可以在这里看到: https ://gist.github.com/anonymous/7760344