【问题标题】:Android SQlite to jsonAndroid SQlite 到 json
【发布时间】:2014-12-29 21:50:46
【问题描述】:

我在我的 android 应用程序的 SQlite 中存储了一些数据,我想将其导出为 json 文件并通过 post HTTP 将其发送到服务器。 您实际上可以在此链接中看到我的代码 - Old question containing my code

在网上搜索后,我只找到了相反的示例(从 json 到 sqlite)。我很乐意得到一些帮助/推动正确的方向。

编辑 -

我在尝试初始化 Gson 时遇到了一个小异常 - 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);

【问题讨论】:

  • 谢谢。处理 myPath=DB_PATH 时,如何知道我的数据库路径?
  • 创建数据库的路径。默认情况下,它类似于 /data/data/com.me.myapp/databases/mydb.db 或者您可以使用 String dbname = "mydb.db"; Path dbpath = ctx.getDatabasePath(dbname);

标签: android json sqlite


【解决方案1】:

使用 GSON 库是迄今为止最简单的处理方式。

1) 初始化一个 ArrayList
2) 从数据库中检索所有数据并将其存储在 ArrayList
3) 使用 gson.toJson(NameOfArrayList);
4) 完成。您现在有一个包含数据库中所有数据的字符串。

【讨论】:

  • 谢谢。听起来确实很容易,但我没有这方面的经验。你能提供一个例子吗?如何将 SQlite 中的数据插入到 ArrayList 中?
  • 我很快把这个 pastebin 和从我自己的一个项目中提取的代码 sn-ps 放在一起:pastebin.com/2WYDvyha 这样,你可以得到一个包含所有数据的 ArrayList,然后你可以只需创建一个 gson 变量并使用它: Gson gson = new Gson(); String jsonString = gson.toJson(arrayList);
  • 谢谢。我有一点例外,我将其添加到主帖中。你能看看吗?
  • 如果您不发布实际的异常消息,我绝对无法确定您的问题。
【解决方案2】:

您是否考虑过使用诸如 Gson 之类的库?它允许您轻松地将 POJO(普通旧 java 对象)转换为 JSON。

您可以简单地从您的数据库中获取您的对象,然后执行以下操作。

MyObject obj = new MyObject();
Gson gson = new Gson();
String json = gson.toJson(obj); 

它非常有用,这里是用户指南的链接: https://sites.google.com/site/gson/gson-user-guide

祝你好运!

【讨论】:

  • 谢谢,不过对我来说有点太复杂了。
【解决方案3】:

下面的过程可能会有所帮助,

public JSONArray QueryAsJSON(String Query)
            throws org.json.simple.parser.ParseException {
        String out = "[";

        try {
            Cursor c = null;

            c = database.rawQuery(Query, null);
            c.moveToFirst();
            String VergCol = "";

            for (int j = 0; j < c.getCount(); j++) {
                out += VergCol + "{";
                String vergRow = "";

                for (int i = 0; i < c.getColumnCount(); i++) {
                    out += vergRow + "\"" + c.getColumnName(i) + "\"" + ":"
                            + "\"" + c.getString(i) + "\"";
                    vergRow = ",";
                }

                out += "}";
                VergCol = ",";
                c.moveToNext();
            }
        } 
        catch (Exception e1) {
            e1.printStackTrace();
        }

        out += "]";

        JSONParser parser = new JSONParser();
        org.json.simple.JSONArray jsonArray = (org.json.simple.JSONArray) parser
                .parse(out);
        return jsonArray;

}

【讨论】:

  • 请不要这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
  • 2015-03-01
相关资源
最近更新 更多