androidjsonkotlinarraylist

How to convert JsonObject to class model kotlin android


I receive some data from server and convert it to JsonArray. I thought that maybe I will be able to convert this array to ordinary array of class objects, but I didn't manage to do it. I tried to do it like that:

val testArray = tpsObject.getAsJsonObject("questions")[tpsSelection[0].toString()].asJsonArray
  for (i in 0 until testArray.size()) {
Log.i("m",Gson().fromJson(testArray.get(i).asJsonObject.toString(),QuestionModel::class.java).question.toString())
   }

but I received error:

 com.google.gson.JsonSyntaxException: Expected a com.google.gson.JsonObject but was com.google.gson.JsonArray

On the other hand I can show all elements of JsonArray like this at loop:

Log.i("m",testArray.get(i).asJsonObject.toString())

I also tried to do it like that:

val jsonParser = JsonParser.parseString(testArray.get(i).asJsonObject.toString())
val model: QuestionModel = Gson().fromJson(jsonParser, QuestionModel::class.java)

but I receive similar error. So maybe someone knows how to solve this problem?

UPDATE

my json:
"1": [
    {
        "answer_options": [
            {
                "id": 216,
                "answer": "some text"
            },
            {
                "id": 217,
                "answer": "some text"
            },
            {
                "id": 218,
                "answer": "some text"
            },
            {
                "id": 219,
                "answer": "some text"
            },
            {
                "id": 220,
                "answer": "some text"
            }
        ],
        "id": 949,
        "question": "some text"
    },
               ...
               {
        "answer_options": [
            {
                "id": 216,
                "answer": "some text"
            },
            {
                "id": 217,
                "answer": "some text"
            },
            {
                "id": 218,
                "answer": "some text"
            },
            {
                "id": 219,
                "answer": "some text"
            },
            {
                "id": 220,
                "answer": "some text"
            }
        ],
        "id": 949,
        "question": "some text"
    },
],

my model class:

class QuestionModel {
    @SerializedName("question")
    @Expose
    var question: String? = null

    @SerializedName("id")
    @Expose
    var id: Int? = null

    @SerializedName("answer_options")
    @Expose
    var answer_options: JsonObject? = null
}

maybe it will help to find a solution


Solution

  • I would suggest that you use kotlinx.serialization instead.

    If you want to stick with your approach: You really need to post your JSON (or a snippet thereof) and class definition, as otherwise it is very hard to help you. The essential problem is that you are feeding Gson an array, but it wanted an object. Perhaps you are actually using a 2d array in the JSON? This would be much easier to debug given a stack trace.

    Also, you appear to be calling .toString() on the JsonObject before you deserialize it, which implies that you are reserializing to Json and then back again... but why?