0

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()
        }
    }

2 Answers 2

1

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

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

Comments

0

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/

4 Comments

for kotlin data class use pojhttps://www.json2kotlin.com/
what i need is just for the jsonArray options. not for the main json its values are just strings. each element has a jsonArray options, thats what i am trying to access
yes you can use the pojo class for the JSON array options ,, also double check the JSON because it is not correct you can use JSON validator to verify it
Yes, thank you, the issue was from the json structure for the options array

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.