当我使用 SQLite 添加数据时,是否可以将数据转换为 JSON,以便在我检索数据时对其进行解析并获取数据。如果可能,请举例说明。
最佳答案
当您从 SQLiteDatabase
获取数据时,它会在 Cursor
中返回。不幸的是,没有这种直接格式可以将数据从游标转换为 JSON,但您可以使用一些代码来完成,例如:
private JSONArray convertCursorToJSON(Cursor cursor) {
JSONArray result = new JSONArray();
int columnCount = cursor.getColumnCount();
while (cursor.moveToNext()) {
JSONObject row = new JSONObject();
for (int index = 0; index < columnCount; index++) {
row.put(cursor.getColumnName(index), cursor.getString(index));
}
result.put(row);
}
cursor.close();
return result;
}
关于android - 将 SQLite 转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32087154/