Al añadir JSONObjects al escribir en un archivo

Descanso :

Estoy tratando de anexar JSONObjectsel interior de un JSONArray que se llama Records.

La primera vez que guardarlo se ahorra de esta manera que está bien

{
  "Records": [
    {
      "travelTime": 2,
      "totalDistance": 0,
      "pace": 0,
      "kCalBurned": 0,
      "latlng": "[lat\/lng: (-32.1521234,-63.66412321)]"
    }
  ]
}

Pero cuando trato de agregar un nuevo otra vez en el interior JSONObject Records, se crea un nuevo JSONArray por ella, y sólo quiero añadir un nuevo objeto en el interior de los registros

{
  "Records": [
    {
      "travelTime": 2,
      "totalDistance": 0,
      "pace": 0,
      "kCalBurned": 0,
      "latlng": "[lat\/lng: (-31.6432292,-63.3667462)]"
    }
  ]
}{
  "Records": [
    {
      "travelTime": 1,
      "totalDistance": 0,
      "pace": 0,
      "kCalBurned": 0,
      "latlng": "[lat\/lng: (-31.9522431,-64.3461241)]"
    }
  ]
}

Este es el código que uso para guardar el Records

   private void writeJsonData(long travelTime,float totalDistance, float pace, float kCalBurned, LinkedList<LatLng> latlng){

        String jsonStr = "";
        JSONObject records  = new JSONObject();
        try {
            records.put("travelTime", travelTime);
            records.put("totalDistance", totalDistance);
            records.put("pace", pace);
            records.put("kCalBurned", kCalBurned);
            records.put("latlng", latlng);

            JSONArray jsonArray = new JSONArray();
            jsonArray.put(records);

            JSONObject recordsObj = new JSONObject();
            recordsObj.put("Records", jsonArray);

            jsonStr = recordsObj.toString();

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String file_name = "records.json";

        FileOutputStream fileOutputStream = null;
        try {

            fileOutputStream = new FileOutputStream(new File(mContext.getFilesDir(),file_name),true);
            fileOutputStream.write(jsonStr.getBytes());
            fileOutputStream.close();


        } catch (IOException e) {
            e.printStackTrace();
        }


    }
Ahsan Ali:

Pasando segundo parámetro a trueen FileOutputStreamel constructor añadirá JSONObject al final del archivo.

Para anexar con matriz JSON en el interior Recordsde objeto, usted tiene que leer el archivo en primer lugar, añada el nuevo objeto JSON y lo escribe de nuevo a archivo.

Uso GSON biblioteca para la conversión entre la clase java y JSON. Por lo que no tiene que crear el objeto JSON manualmente cada vez poniendo cada par de claves.

Crear una clase Java para sostener todo Recordsobjeto

public class Record
{
    @SerializedName("Records")
    private List<Object> recordsList;

    public Record()
    {
        this. recordsList = new ArrayList<>();
    }

    public List<Object> getRecordsList()
    {
        return recordsList;
    }
}

Ahora cree JAVA Model classpara contener información de viajes

public class Travel {

    private Integer travelTime;
    private Integer totalDistance;
    private Integer pace;
    private Integer kCalBurned;
    private LinkedList<LatLng> latlng;

    public Integer getTravelTime() {
        return travelTime;
    }

    public void setTravelTime(Integer travelTime) {
        this.travelTime = travelTime;
    }

    public Integer getTotalDistance() {
        return totalDistance;
    }

    public void setTotalDistance(Integer totalDistance) {
        this.totalDistance = totalDistance;
    }

    public Integer getPace() {
        return pace;
    }

    public void setPace(Integer pace) {
        this.pace = pace;
    }

    public Integer getKCalBurned() {
        return kCalBurned;
    }

    public void setKCalBurned(Integer kCalBurned) {
        this.kCalBurned = kCalBurned;
    }

    public LinkedList<LatLng> getLatlng() {
        return latlng;
    }

    public void setLatlng(LinkedList<LatLng> latlng) {
        this.latlng = latlng;
    }

}

Aquí está la clase de servicio con una función para añadir nueva JSON en el interior Recordsdel objeto. Se comprobará si el directorio y archivo se crean lo contrario creará existen archivos both.If, se leerá el archivo, añada el nuevo objeto JSON a la lista y escribir de nuevo en el mismo archivo. Puede cambiar el nombre del directorio y archivo con la suya.

Nota: Esta clase está escrito en Kotlin. Aquí se hace referencia como configurar Android Estudio de Kotlin

class Logger {

    companion object {

        private const val LOG_FILE_FOLDER = "Logs"
        private const val LOG_FILE_NAME = "transaction"
        private const val DATE_FORMAT = "yyyy-MM-dd"
        private val logFileName: String
            @SuppressLint("SimpleDateFormat")
            get() {

                var fileName = LOG_FILE_NAME
                val dateFormat = SimpleDateFormat(DATE_FORMAT)
                fileName += "_" + dateFormat.format(Date()) + ".json"
                return fileName
            }

fun logFile(json: Any) {

try {
    val directoryPath = Environment.getExternalStorageDirectory().path + "/" + LOG_FILE_FOLDER
    val loggingDirectoryPath = File(directoryPath)
    var loggingFile = File("$directoryPath/$logFileName")
    if (loggingDirectoryPath.mkdirs() || loggingDirectoryPath.isDirectory) {
        var isFileReady = true
        var isNewFile = false
        if (!loggingFile.exists()) {
            isFileReady = false
            try {
                loggingFile.createNewFile()
                isNewFile = true
                isFileReady = true
            } catch (e: Exception) {
                e.printStackTrace()
            }

        } else {
            val lastFile = getLastFile(loggingFile.name, directoryPath)
            loggingFile = File("$directoryPath/$lastFile")
            val fileSize = getFileSize(loggingFile)

        }
        if (isFileReady) {

            var jsonString: String? = null

            if (!isNewFile) {

                //Get already stored JsonObject
                val stream = FileInputStream(loggingFile)


                try {
                    val fileChannel = stream.channel
                    val mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size())

                    jsonString = Charset.defaultCharset().decode(mappedByteBuffer).toString()
                } catch (e: Exception) {
                    e.printStackTrace()
                } finally {
                    stream.close()
                }
            }


            //Create record object
            val record = if (!jsonString.isNullOrEmpty()) {
                Gson().fromJson(jsonString, Record::class.java)
            } else {
                Record()
            }


            //Append the current json
            record.recordList.add(json)

            //create json to save
            val jsonToSave = Gson().toJson(record)

            val bufferedOutputStream: BufferedOutputStream
            try {
                bufferedOutputStream = BufferedOutputStream(FileOutputStream(loggingFile))
                bufferedOutputStream.write(jsonToSave.toByteArray())
                bufferedOutputStream.flush()
                bufferedOutputStream.close()

            } catch (e4: FileNotFoundException) {
                e4.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            } finally {
                System.gc()
            }
        }
    }
} catch (ex: Exception) {
    ex.printStackTrace()
}
}
}
}

Al final, se puede registrar el archivo con logFileel método

Logger.Companion.logFile(travel);

Salud :)

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=207588&siteId=1
Recomendado
Clasificación