【发布时间】:2019-09-20 07:43:05
【问题描述】:
我从我的应用程序中的 ajax 调用接收到 JSONArray 的数据。在我的应用程序中的 SQLite 表上执行插入/冲突跳过的最佳方法是什么?我有以下代码:
try {
JSONArray data = response.getJSONArray("data");
ContentValues contentValues = new ContentValues();
// I don't know what to do here.
// Do I do a for loop and convert the entire JSONArray into ContentValues
// with the column name and value for each item in the array?
db.insertWithOnConflict("Data", null, data, SQLiteDatabase.CONFLICT_IGNORE);
} catch (JSONException e) {
e.printStackTrace();
}
【问题讨论】:
-
所以,网络和数据库是两个不同的东西,你应该养成不要混用它们的习惯。话虽如此,序列化您的响应可能更容易,因此您不必手动搜索
JSONArray和JSONObject中的字段。假设您确实想继续反对我给您的两个建议,JSONArrays 是一个列表,因此它们没有每个项目的名称(这将是一个地图)。循环 JSONArray 就像循环 List 一样(尽管有一些限制): for(int i = 0; i
标签: java android arrays sqlite insert