0

Right now, my code looks like this AddActivity.kt

fun addCarToJSON(brand: String, model: String, year: Int, color: String, type: String, price: Double) {
        // TODO: finish function to append data to JSON file

        var carlist: CarList = CarList(brand, model, year, color, type, price)
        val gsonPretty = GsonBuilder().setPrettyPrinting().create()
        val newCarInfo: String = gsonPretty.toJson(carlist)
        saveJSON(newCarInfo)
    }

    fun saveJSON(jsonString: String) {
        val output: Writer
        val file = createFile()
        output = BufferedWriter(FileWriter(file))
        output.write(jsonString)
        output.close()
    }

    private fun createFile(): File {
        val fileName = "carlist.json"
        val storageDir = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)
        if (storageDir != null) {
            if (!storageDir.exists()){
                storageDir.mkdir()
            }
        }

        return File(
                storageDir,
                fileName
        )
    }

This outputs the following, as the user inputs:

{
  "brand": "Toyota",
  "color": "Red",
  "model": "Prius",
  "price": 6.0,
  "type": "Hatchback",
  "year": 2009
}

However, I am trying to get the data to save as a JSON array, like so:

[
  {
    "brand": "Toyota",
    "model": "RAV4",
    "year": 2016,
    "color": "red",
    "type": "SUV",
    "price": 27798.0
  },
  {
    "brand": "Mitsubishi",
    "model": "Lancer",
    "year": 2010,
    "color": "grey",
    "type": "sedan",
    "price": 10999.0
  }
]

For reference, here is CarList.kt (a class)

class CarList(val brand: String, val model: String, val year: Int, val color: String, val type: String, val price: Double) {
}

How do I change my code to output as the format I desire, a JSON array?

Noren
  • 30
  • 6
  • Does this answer your question? [How to print this type of json Array output in android](https://stackoverflow.com/questions/44135708/how-to-print-this-type-of-json-array-output-in-android) – a_local_nobody Jan 06 '21 at 07:16
  • 1
    You need to create a list of Cars, and serialize it to JSON, your `CarList` class should be named `Car`. and your `carList` object should be created as list of `Car`. Ex. `var carlist= listOf( Car(brand1, model1, year1, color1, type1, price1), Car(brand2, model2, year2, color2, type2, price2) )` – mhdwajeeh.95 Jan 06 '21 at 07:34
  • 1
    @mhdwajeeh.95 thank you for that! I've gotten the basics of the function working from this :) – Noren Jan 07 '21 at 00:59

1 Answers1

0

To format this correctly, replace

var carlist: CarList = CarList(brand, model, year, color, type, price)

With

var carlist = listOf(CarList(brand, model, year, color, type, price))

This will save the data in the following format: carlist.json

[
  {
    "brand": "Toyota",
    "color": "Red",
    "model": "Prius",
    "price": 6.0,
    "type": "Hatchback",
    "year": 2009
  }
]

To add more entries, format the data like this:

var carlist = listOf(
  CarList(brand1, model1, year1, color1, type1, price1),
  CarList(brand2, model2, year2, color2, type2, price2)
)

This will save in the correct format of a JSON array.

Noren
  • 30
  • 6