使用getter和setter将json转换为构造函数

时间:2019-02-01 03:56:51

标签: json sqlite android-studio getter-setter

将数据转换为json以进行翻新时遇到了一些麻烦。这就是我的工作方式。

我的sqlite数据库中有一堆这样的数据:

enter image description here

我要做的就是获取所有数据并将其转换为json,如下所示: (PS。我在此链接中有此代码。https://tech.sarathdr.com/convert-database-cursor-result-to-json-array-android-app-development-1b9702fc7bbb

  public void getdbAndSendData() {

    Cursor cursor2=databaseHelper.selectLocationFromLocalDatabaseAll(db);
    JSONArray resultSet = new JSONArray();
    JSONObject returnObj = new JSONObject();


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

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

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

                try {

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

        }

        resultSet.put(rowObject);
        cursor2.moveToNext();
    }

    cursor2.close();
    Log.d("PAkultie", resultSet.toString());

这是我的应用在调试器中的结果:

enter image description here

由于我已经正确获取了数据,所以我希望有一个getter setter构造函数,以便它可以将改造作为其数据进行调用并进行回调。

无论如何,这是我以前的getter setter构造函数。 (PS。此getter设置器将行数据存储在我的sqlite中,并在进行改造和工作时将其发送。 但我必须更新我的数据,该数据可以包含很多行,以便在一次调用中进行翻新发送)

     public class MapDetails {
         @SerializedName("SerialNumber")
         @Expose
         private String SerialNumber;
         @SerializedName("Coordinate1")
         @Expose
         private String Coordinate1;
         @SerializedName("Coordinate2")
         @Expose
         private String Coordinate2;
         @SerializedName("DateTime")
         @Expose
         private String DateTime;
         @SerializedName("Speed")
         @Expose
         private String Speed;
         @SerializedName("Port")
         @Expose
         private int Port;

    public MapDetails(String serialNumber, String coordinate1, String coordinate2, String dateTime, String speed, int port) {
        SerialNumber = serialNumber;
        Coordinate1 = coordinate1;
        Coordinate2 = coordinate2;
        DateTime = dateTime;
        Speed = speed;
        Port = port;
    }

    public String getSerialNumber() {
        return SerialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        SerialNumber = serialNumber;
    }

    public String getCoordinate1() {
        return Coordinate1;
    }

    public void setCoordinate1(String coordinate1) {
        Coordinate1 = coordinate1;
    }

    public String getCoordinate2() {
        return Coordinate2;
    }

    public void setCoordinate2(String coordinate2) {
        Coordinate2 = coordinate2;
    }

    public String getDateTime() {
        return DateTime;
    }

    public void setDateTime(String dateTime) {
        DateTime = dateTime;
    }

    public String getSpeed() {
        return Speed;
    }

    public void setSpeed(String speed) {
        Speed = speed;
    }

    public int getPort() {
        return Port;
    }

    public void setPort(int port) {
        Port = port;
    }

}

仅供参考:这也是我以前在该构造函数上的代码。

   Cursor cursor = databaseHelper.retrieveLocationFromLocalDatabase(db);
          while (cursor.moveToNext()) {
              serialID =ursor.getString(cursor.getColumnIndex
  (DatabaseHelper.SERIALNUMBER));
        lati = cursor.getString(cursor.getColumnIndex
  (DatabaseHelper.LATITUDE));
        longi = cursor.getString(cursor.getColumnIndex
  (DatabaseHelper.LONGITUDE));
        dateTime = cursor.getString(cursor.getColumnIndex
  (DatabaseHelper.DATE_TIME));
        speed = cursor.getString(cursor.getColumnIndex
  (DatabaseHelper.SPEED));
        portss = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.PORT));
    }

    MapDetails mapDetails = new MapDetails(serialID,
   lati, longi, dateTime, speed, portss);
    List<MapDetails> data = new ArrayList<>();
    data.add(mapDetails);


    try {
        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl("http://" + ADDRESS + ":" + PORT)  // this cotains my api link
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit = builder.build();

        API locate = retrofit.create(API.class);

1 个答案:

答案 0 :(得分:1)

尝试查看我的代码,我已经意识到我也没有设置我的arraylist的大小,我发现可以在json对象上添加数据以拥有json数组,然后进行改装通过剩余的json数组发送。.

首先,我已经设置了arraylist,以便可以存储更多的json对象:

  data = new ArrayList<>(20);

第二,我每2秒获取一次的数据将被添加到我的detail / jsonObject中:

  MapDetails mapDetails = new MapDetails(id, lat, lon,well,"2", 9090);
   data.add(mapDetails);

这是我现在的新数据,在调试器中找到的json对象中: (从JsonObject到Json Array)

enter image description here

,您现在可以通过改装发送... 及其现在的工作:)