从源码读取数据和在存储JSON

问题描述 投票:1回答:2

我有我想获取代表特定列的所有数据,然后要保存数据以JSON的形式,这样我可以通过API将其发送至数据库保存的表。

我无法从数据库中获取的所有数据,虽然我试图做它通过光标我在这个越来越单一的数据。请帮我在获取数据,并将其转换成JSON。这是我所编码。这是从中延伸Dbsave DatabaseOpenHelper类中的方法。

public Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+"autosave",null);
    return res;
}

然后,我使用这样的活动这种方法。

public void getalldata(){
  cursor=dbAutoSave.getAllData();
  if (cursor!=null) {
      if(cursor.moveToNext()) {
          for (int i = 0; i <= cursor.getCount(); i++) {
              aaa = cursor.getString(1);
              String bbb = cursor.getString(2);
              String ccc = cursor.getColumnName(3);
          }

          ArrayList<String> aaaa=new ArrayList<>();
          aaaa.add(aaa);
          Toast.makeText(getApplicationContext(),"bbbbb"+aaaa,Toast.LENGTH_LONG).show();
      }
      cursor.close();
  }
}

我收到只有一个aaaa数据。然后我试图与gettersetter但没有好处这样做。

private void showEmployeesFromDatabase() {
   Cursor cursorEmployees = mDatabase.rawQuery("SELECT * FROM autosave", null);
   if (cursorEmployees.moveToFirst()) {
       do {
           // Pushing each record in the employee list
           employeeList.add(new SetterGetter(
                   cursorEmployees.getString(0),
                   cursorEmployees.getString(1),
                   cursorEmployees.getString(2)
           ));
       } while (cursorEmployees.moveToNext());
   }

   // Closing the cursor
   System.out.println("aaaaaa" + employeeList.get(1));
   cursorEmployees.close();
}

我无法从settergetter列表分析数据。如果我将能够获取所有数据,我会用GSON将其转换成JSON。

java android sqlite gson getter-setter
2个回答
2
投票

getalldata函数内环路故障。这不是遍历光标,只是遍历相同元素一次又一次。我想建议更改类似下面的功能。

public void getalldata() {

  // Cursor is loaded with data
  cursor = dbAutoSave.getAllData();
  ArrayList<String> aaaa = new ArrayList<>();

  if (cursor != null) {
      cursor.moveToFirst();

       do {
          aaa = cursor.getString(1);
          String bbb = cursor.getString(2);
          String ccc = cursor.getColumnName(3);

          // Add into the ArrayList here
          aaaa.add(aaa);

       } while (cursor.moveToNext());

       cursor.close();
   }
}

希望解决您的问题。

更新

要转换使用GSON存储在ArrayList到JSON数据,您需要先添加库在build.gradle文件。你可以找到使用它here的一种方式。

只需添加在你的build.gradle文件中的以下的依赖。

dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}

GSON接受一个对象将其转换为JSON。所以,我想建议你创建与你的光标像下面取出的元素的对象。

public class Data {
    public String aaa;
    public String bbb;
    public String ccc;
}

public class ListOfData {
    public List<Data> dataList;
}

现在再次修改功能如下所示。

public void getalldata() {

  // Cursor is loaded with data
  cursor = dbAutoSave.getAllData();
  ArrayList<Data> dataList = new ArrayList<Data>();

  if (cursor != null) {
      cursor.moveToFirst();

       do {
          Data data = new Data();
          data.aaa = cursor.getString(1);
          data.bbb = cursor.getString(2);
          data.ccc = cursor.getColumnName(3);

          // Add into the ArrayList here
          dataList.add(data);

       } while (cursor.moveToNext());

       // Now create the object to be passed to GSON
       DataList listOfData = new DataList();
       listOfData.dataList = dataList;

       Gson gson = new Gson();
       String jsonInString = gson.toJson(listOfData); // Here you go! 

       cursor.close();
   }
}

0
投票

要初始化您的数组列表光标内每次初始化它的游标外

public void getalldata(){
  cursor=dbAutoSave.getAllData();
 ArrayList<String> aaaa=new ArrayList<>();
  if (cursor!=null) {
      if(cursor.moveToNext()) {
          for (int i = 0; i <= cursor.getCount(); i++) {
              aaa = cursor.getString(1);
              String bbb = cursor.getString(2);
              String ccc = cursor.getColumnName(3);
          }


          aaaa.add(aaa);
          Toast.makeText(getApplicationContext(),"bbbbb"+aaaa,Toast.LENGTH_LONG).show();
      }
      cursor.close();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.