I have a list in my POJO class:
"userQuoteTravellers": [
{
"id": 1354,
"quoteId": 526,
"travellerId": null
}
]
I want to pass this list as it is in JSONArray and passing it as:
JSONArray.put(list)
It is being sent as:
"userQuoteTravellers": [ "[]" ]
But I want to send it as
"userQuoteTravellers": []
How can I achieve this in Kotlin without using any loop?
put adds the list as an element to the JSONArray. Thats not what you want. You want your JSONArray to represent the list.
JSONArray offers a constructor for that:
val jsonArray = JSONArray(listOf(1, 2, 3))
But there is a much easier way. You don't need to worry about single properties. Just pass the whole POJO.
Let's say you have this:
class QuoteData(val id: Int, val quoteId: Int, travellerId: Int?)
class TravelerData(val userQuoteTravellers: List<QuoteData>)
val travelerData = TravelerData(listOf(QuoteData(1354, 546, null)))
You just have to pass travelerData to the JSONArray constructor:
val travelerDataJson = JSONArray(travelerData)
and it will be represented like this:
"userQuoteTravellers": [ { "id": 1354, "quoteId": 526, "travellerId": null } ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With