android - 将 SQLite 转换为 JSON

标签 android json

当我使用 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/

相关文章:

android - 强制开启安卓蓝牙发现

javascript - 如何将 JavaScript 文字转换为 JSON 数组?

jquery - JSONP请求错误处理

javascript - 我可以在cordova应用程序中将配置从config.xml写入AndroidManifest.xml吗

java - 检索动态弹出菜单项

java - 解析包含不同对象列表的 JSON 文件

java - 获取嵌套的 JSON 对象值 - Java SimpleJson

json - PostgreSQL 将多行转换为 json_build_object 中的 json 数组

android - 在 DBFLow 中排序

android - 确保 Android 应用程序是从 Play 商店安装的