【问题标题】:creating Json from sqlite in Android在 Android 中从 sqlite 创建 Json
【发布时间】:2013-12-27 13:50:34
【问题描述】:

我想从我的 sqlite 中获取 json 文件并将其保存在asset.myJson.json 中。 以下是我的代码,我想在 myJson.json 中查看结果。请帮助我如何将数据导入 myJson.json。

private JSONArray jsongetResult(){
    SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
    Cursor cursor = database.rawQuery("SELECT id,title,qty,price FROM CART;", null);

    JSONArray resultSet     = new JSONArray();

    cursor.moveToFirst();
    while (cursor.isAfterLast() == false) {

        int totalColumn = cursor.getColumnCount();
        JSONObject rowObject = new JSONObject();

        for( int i=0 ;  i< totalColumn ; i++ )
        {
            if( cursor.getColumnName(i) != null )
            {

                try
                {

                    if( cursor.getString(i) != null )
                    {
                        Log.d("TAG_NAME", cursor.getString(i) );
                        rowObject.put(cursor.getColumnName(i) ,  cursor.getString(i) );
                    }
                    else
                    {
                        rowObject.put( cursor.getColumnName(i) ,  "" );
                    }
                }
                catch( Exception e )
                {
                    Log.d("TAG_NAME", e.getMessage()  );
                }
            }

        }

        resultSet.put(rowObject);

    }
    cursor.close();
    Log.d("TAG_NAME", resultSet.toString() );
    return resultSet;

}

【问题讨论】:

  • 你想写(resultSet)到文件
  • 你是在 json 对象中获取数据吗
  • @VishalMokal,我的目的是查看代码是否正确。(将 sqlite 转换为 json)。所以我想在 myJson.json 中查看结果
  • 检查答案是否对您有帮助

标签: android json android-layout android-listview


【解决方案1】:

将此行放入您的 jsongetResult() 函数中

File f = new File("your path"); FileOutputStream fos = new FileOutputStream(f,true); PrintStream ps = new PrintStream(fos);

pa.append(resultSet.toString());

这就是我在 json 中转换数据的方式

public String getAllDataAndGenerateJSON() throws JSONException, FileNotFoundException {

    String query = "select " + NAME + "," + ADDRESS + "," + CITY + ","
            + CONTACTNO + "," + AVAILABLE + "," + CATEGORY
            + " from contact_list";
    Cursor c = database.rawQuery(query, null);
    c.moveToFirst();
    JSONObject Root = new JSONObject();
    JSONArray ContactArray = new JSONArray();
    File f = new File(Environment.getExternalStorageDirectory()
            + "/ContactDetail.txt");
    FileOutputStream fos = new FileOutputStream(f,true);
    PrintStream ps = new PrintStream(fos);


    int i = 0;
    while (!c.isAfterLast()) {


            JSONObject contact = new JSONObject();
            try {
                contact.put("Name", c.getString(c.getColumnIndex(NAME)));
                contact.put("Address", c.getString(c.getColumnIndex(ADDRESS)));
                contact.put("City", c.getString(c.getColumnIndex(CITY)));
                contact.put("ContactNumber", c.getString(c.getColumnIndex(CONTACTNO)));
                contact.put("Available", c.getString(c.getColumnIndex(AVAILABLE)));
                contact.put("Category", c.getString(c.getColumnIndex(CATEGORY)));

                c.moveToNext();

                ContactArray.put(i, contact);
                i++;

            } catch (JSONException e) {

                e.printStackTrace();
            }



        }
        Root.put("CONTACTDETAILS", ContactArray);
        ps.append(Root.toString());
        return Root.toString();
    }

【讨论】:

    【解决方案2】:

    我使用字符串连接运算符在我的原始查询的 select 子句中构建我的 JSON (||) 并且它有效。

    而不是

    SELECT json_object('column1', column1, 'column2', column2) FROM table
    

    使用

    SELECT "{" || "column1"  || ":" || column1 || || "," || "column2"  || ":" || column2 ||  "}" FROM table
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      相关资源
      最近更新 更多