Я получаю некоторые данные с сервера и преобразую их в JsonArray. Я думал, что, возможно, я смогу преобразовать этот массив в обычный массив объектов класса, но мне не удалось это сделать. Я пытался сделать это так:
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())
}
, но я получил ошибку:
com.google.gson.JsonSyntaxException: Expected a com.google.gson.JsonObject but was com.google.gson.JsonArray
С другой стороны, я могу показать все элементы JsonArray следующим образом: l oop:
Log.i("m",testArray.get(i).asJsonObject.toString())
Я также пытался сделать это так:
val jsonParser = JsonParser.parseString(testArray.get(i).asJsonObject.toString())
val model: QuestionModel = Gson().fromJson(jsonParser, QuestionModel::class.java)
, но я получаю похожую ошибку. Так может кто знает, как решить эту проблему?
ОБНОВЛЕНИЕ
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"
},
],
мой класс модели:
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
}
возможно, это поможет найти решение