9

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?

3

5 Answers 5

6

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 } ]

Sign up to request clarification or add additional context in comments.

Comments

6

With Dependencies

Add to your gradle:

implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

Convert ArrayList to JsonArray

val jsonElements = (JsonArray) new Gson().toJsonTree(itemsArrayList)

Without Dependencies

val jsonElements = JSONArray(itemsArrayList)

4 Comments

Is this using the same JSON library as the original question is asking about?
Yes, But If you don't want to use library than default is val jsArray = JSONArray(mylist). Just pass your generic array list to JSONArray arguments
Here JSONArray is come from Collection only. No need to add library for that
@KalpeshRupani No, it will not solve my problem because I have already tried in the same way. Read the question again.
3

If I read the JSONArray constructors correctly, you can build them from any Collection (arrayList is a subclass of Collection) like so:

val list = ArrayList<String?>()
list.add("jigar")
list.add("patel")
val jsArray = JSONArray(list)

You can also use GSON for read json see below example:

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
class userQuoteTravellers {
    @SerializedName("id")
    @Expose
    var id: Int? = null
    @SerializedName("quoteId")
    @Expose
    var quoteId: Int? = null
    @SerializedName("travellerId")
    @Expose
    var travellerId: Any? = null
}

Comments

1

You can achieve this by using this

  implementation 'com.squareup.retrofit2:converter-gson:2.3.0'


  var gson = Gson()
  var jsonData = gson.toJson(PostPojo::class.java)

Comments

1

try this: val userQuote = response.getJSONArray("userQuoteTravellers")

then call the data inside like this:

for (i in 0 until userQuote.length()) {
    val quotes = userQuote.getJSONObject(i)
    // then call the other data here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.