我在我的 Android 应用程序中的 SQlite 中存储了一些数据,我想将其导出到 json 文件中并通过 HTTP 后发送到服务器。 您实际上可以在此链接中看到我的代码 - Old question containing my code
在网络上搜索后,我只找到了以相反方式进行的示例(从 json 到 sqlite)。我会很高兴收到一些帮助/插入正确的方向。
编辑-
我在尝试初始化 Gson 时得到了一个小异常(exception) - String json = gson.toJson(Record); .你能看看吗 -
public ArrayList<Record> getAllRecordsArray() {
ArrayList<Record> localList = new ArrayList<Record>();
String selectQuery = "SELECT * FROM " + TABLE_RECORD;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//Loops through all rows and adds them to the local list
if(cursor.moveToFirst())
{
do {
//Get record information
Record record = new Record();
record.setpLat(cursor.getDouble(0));
record.setpLong(cursor.getDouble(1));
record.setpAcc(cursor.getFloat(2));
record.setpTime(cursor.getLong(2));
//Add record to list
localList.add(record);
} while (cursor.moveToNext());
}
return localList;
}
Gson gson = new Gson();
String json = gson.toJson(Record);
最佳答案
使用 GSON 库是迄今为止处理它的最简单方法。
1) 初始化一个ArrayList
2) 从数据库中检索所有数据并将其存储在 ArrayList
3) 使用 gson.toJson(NameOfArrayList);
4)完成。您现在有一个包含数据库中所有数据的字符串。
关于Android SQlite 到 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27696566/