【问题标题】:Android Kotlin Volley How to get value from JSONArrayAndroid Kotlin Volley 如何从 JSONArray 中获取价值
【发布时间】:2021-09-28 11:40:32
【问题描述】:

我想通过 Volley for 循环在“media_gallery_entries”JSONArray 中获取“文件”

排球

val item = ArrayList<RecyData>()
val jsonRequest = object : JsonObjectRequest(Request.Method.GET, url, null,

    Response.Listener { response ->

       try {

            val jsonArrayItems = response.getJSONArray("items")
            val jsonSize = jsonArrayItems.length()

            for (i in 0 until jsonSize) {
                val jsonObjectItems = jsonArrayItems.getJSONObject(i)
                val pName = jsonObjectItems.getString("name")
                val pPrice = jsonObjectItems.getInt("price")
                item.add(RecyData(pName, pPrice, pImage))
            }
       } catch (e: JSONException) {
             e.printStackTrace()
       }

数据

{
   "items": [
       {
           "id": 1,
           "sku": "10-1001",
           "name": "item01",
           "price": 100,
           "media_gallery_entries": [
               {
                   "id": 1,
                   "file": "//1/0/10-28117_1_1.jpg"
               }
           ]
       }
   ]
}

【问题讨论】:

    标签: android arrays json kotlin android-volley


    【解决方案1】:

    您可以使用一些解析库,例如 Gson,而不是手动解析总是容易出错的 Gson,它经过充分测试并且不太可能导致任何问题

    要首先使用Gson,您需要在 build.gradle 中添加依赖项

    implementation 'com.google.code.gson:gson:2.8.7'
    

    现在定义映射到 JSON 响应的 kotlin 类型

    class Media(
            val id: Int,
            val file: String
    )
    
    class Entry(
            val id: Int,
            val sku: String,
            val name: String,
            val price: Int,
            val media_gallery_entries: List<Media>
    )
    

    现在响应监听器就可以了

    try{
        val jsonArrayItems = response.getJSONArray("items")
        val token = TypeToken.getParameterized(ArrayList::class.java, Entry::class.java).type
        val result:List<Entry> = Gson().fromJson(jsonArrayItems.toString(), token)
        // Do something with result
    }
    catch (e: JSONException) {
        e.printStackTrace()
    }
    

    【讨论】:

      【解决方案2】:

      val pPrice = jsonObjectItems.getInt("price")这一行之后试试这段代码

       val mediaEntryArr = jsonObjectItems.getJSONArray("media_gallery_entries")
                      for(j in 0 until mediaEntryArr.length()){
                          val mediaEntryObj = mediaEntryArr.getJSONObject(j)
                          val id = mediaEntryObj.getString("id")
                          val file = mediaEntryObj.getString("file")
                          Log.e("mediaEntry----",""+ Gson().toJson(mediaEntryObj))
                          Log.e("id----",""+ id)
                          Log.e("file----",""+ file)
                      }
      

      【讨论】:

      • 是的,items 数组的外循环media_gallery_entries 数组的内循环
      • 为什么不使用Gson 来解析整个响应
      猜你喜欢
      • 2021-03-16
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多