如何在 kotlin Android 中访问更大的 jsonArray json 中的 json 数组中的字符串

Posted

技术标签:

【中文标题】如何在 kotlin Android 中访问更大的 jsonArray json 中的 json 数组中的字符串【英文标题】:How do i access the strings in a json array in a bigger jsonArray json in kotlin Android 【发布时间】:2021-10-07 00:21:00 【问题描述】:

我在 android studio 中有一个 json 文件,它是一个 json 对象数组,在每个对象中都有一个名为 options 的 json 数组,每个数组只有字符串,而不是一个对象。

如何访问选项 json 数组中字符串的值,以便将其设置为文本视图的文本。

我尝试过 optionsJsonArry.getString(index),这会返回整个数组。 我需要获取数组中的各个字符串。

这就是我的 json 文件的样子

 
  "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"
        
      ]
    ,

我正在尝试在这里获取选项 jsonarray 中的字符串

这是我尝试过的

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

【问题讨论】:

【参考方案1】:

问题在于 json 的结构,我只是比较了一下。 它应该看起来像这样

  
  "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"
      ]
    ,

方括号后面的大括号不应该出现在选项数组中。 现在 ParentJsonArray[position].optionsArray.getString(index) 用于检索字符串

【讨论】:

【参考方案2】:

这个 si To Use Gson 的最佳解决方案,它是一个 Java 库,可用于将 Java 对象转换为其 JSON 表示。它还可用于将 JSON 字符串转换为等效的 Java 对象。 Gson 可以处理任意 Java 对象,包括您没有源代码的预先存在的对象。 检查此链接以获取更多详细信息: https://github.com/google/gson

您可以使用该网站将 JSON 转换为 Java 类,还有其他可用于 KOtlin 的类: https://www.jsonschema2pojo.org/

【讨论】:

对于 kotlin 数据类使用 pojhttps://www.json2kotlin.com/ 我需要的只是 jsonArray 选项。不适用于主 json,它的值只是字符串。每个元素都有一个 jsonArray 选项,这就是我要访问的内容 是的,您可以将 pojo 类用于 JSON 数组选项,还可以仔细检查 JSON,因为它不正确,您可以使用 JSON 验证器来验证它 是的,谢谢,问题出在 options 数组的 json 结构中

以上是关于如何在 kotlin Android 中访问更大的 jsonArray json 中的 json 数组中的字符串的主要内容,如果未能解决你的问题,请参考以下文章