I am downloading a JSON Object and storing it in a data class. I am able to download the data and successfully and store it in the data class. I want to store all the values inside the option
field (which is an array) in the JSON object into a separate ArrayList
.
JsonSchema
{8 items
"area":"sat"
"author":"twinword inc."
"email":"help@twinword.com"
"level":3
"quizlist":[10 items
0:{3 items
"correct":2
"option":[2 items
0:"jury"
1:"assess"
]
"quiz":[3 items
0:"value"
1:"estimate"
2:"evaluate"
]
}
1:{...}3 items
2:{...}3 items
3:{...}3 items
4:{...}3 items
5:{...}3 items
6:{...}3 items
7:{...}3 items
8:{...}3 items
9:{...}3 items
]
"result_code":"200"
"result_msg":"Success"
"version":"4.0.0"
}
DataClasses
data class Game2Model(@SerializedName("area") var area : String,
@SerializedName("level") var level : Int,
@SerializedName("quizlist") var quizlist : List<QuizData>,
@SerializedName("version") var version : String,
@SerializedName("author") var author : String,
@SerializedName("email") var email : String,
@SerializedName("result_code") var resultCode : String,
@SerializedName("result_msg") var resultMsg : String ){}
data class QuizData( @SerializedName("quiz") var quiz : List<String>,
@SerializedName("option") var option : List<String>,
@SerializedName("correct") var correct : Int){}
If you need a list of all the options in a single bigger list, you could flatMap
function in the following way:
val allOptions = game2Model.quizlist.flatMap { q -> q.option }