Reputation: 93
I have a json file in android studio, it's a json array of objects, in each object there's a json array called options that has just strings each, not an object.
How can i access the value of the strings in options json array so i can set it as text of a text view.
I've tried doing optionsJsonArry.getString(index), this returns the whole array. I need to get the individual strings in the array.
This what my json file looks like
{
"questions":[
{
"id":"1",
"question_text":"1. What is your gender?",
"description_text":"1. What is your gender?",
"options":[
{
"option_1":"Male",
"option_2":"Female",
"option_3":"Not necessary"
}
]
},
{
"id":"2",
"question_text":"2. In what year were you born?",
"description_text":"2. In what year were you born?",
"options":[
{
"option_1":"Yes",
"option_2":"No",
"option_3":"Not Sure"
}
]
},
i am trying to get the strings in options jsonarray here
This what i have tried
fun getPraQuestions(file: InputStream) {
val questionData = arrayListOf<PraModel>()
val optionList = arrayListOf<String>()
try {
val obj = JSONObject(loadJSONFromAsset(file))
val jsonArray = obj.getJSONArray("questions")
Log.d("json array Length", jsonArray.length().toString())
for (i in 0 until jsonArray.length()) {
val singleJson = jsonArray.getJSONObject(i)
val id = singleJson.getString("id")
val questionText = singleJson.getString("question_text")
val options = singleJson.getJSONArray("options")
val optionObj = JSONArray()
val gottenArray = optionObj.getJSONArray("options")
// val gson = Gson()
// val convert: List<String> = gson.fromJson(options,Array<String>::class.java)
val descriptionText = singleJson.getString("description_text")
Log.d("optionList array Length", optionList.size.toString())
questionData.add(PraModel(id, questionText, descriptionText, options = options))
}
_praQuestions.value = questionData
} catch (e: JSONException) {
e.printStackTrace()
}
}
Upvotes: 0
Views: 1379
Reputation: 93
The issue is with the structure of the json, i just compared. it should look like this instead
{
"questions":[
{
"id":"1",
"question_text":"1. What is your gender?",
"description_text":"1. What is your gender?",
"options":[
"Male",
"Female",
"Not necessary"
]
},
{
"id":"2",
"question_text":"2. In what year were you born?",
"description_text":"2. In what year were you born?",
"options":[
"Yes",
"No",
"Not Sure"
]
},
The curly braces after the square brackets should not be there for the options array. Now ParentJsonArray[position].optionsArray.getString(index) works for retrieving the string
Upvotes: 1
Reputation: 31
the Best solution for this si To Use Gson which is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source code of. check this link for more details : https://github.com/google/gson
you can use this website to convert the JSON to Java Class there is others can be used for KOtlin also : https://www.jsonschema2pojo.org/
Upvotes: 0