简体   繁体   English

如何在 for 循环中使用动态键值从 JSON 数组中获取值? KOTLIN

[英]How to get values from JSON array with dynamic key-values in a for loop ? KOTLIN

This is my JSON:这是我的 JSON:

{"result": {
"product_id": 283946,
"shop_id": 129, 
"is_favorite": false,
"item_sold_by": "Quantity",
"item_description": "hello",
"item_size": [
  {
    "name": "Height",
    "value": "29cm"
  },
  {
    "name": "Width",
    "value": "52cm"
  }
 val json = String(responseBody, charset)

                val gson = Gson().newBuilder().serializeNulls().create()

                val itemsdetailresult = gson.fromJson<ItemDetailJson>(json, genericType<ItemDetailJson>())


                Log.v("Test1", json)

                Log.v("Test2", itemsdetailresult.toString())


                Picasso.get().load(itemsdetailresult.result.pic1).into(itempic)
                itemName.text = itemsdetailresult.result.item_name
                shopName.text = itemsdetailresult.result.shop_name
                itemdescrip.text = itemsdetailresult.result.item_description
                itemprice.text = itemsdetailresult.result.price





                val obj = JSONObject(json)
                val getObject = obj.getJSONObject("result")
                val getArray = getObject.getJSONArray("item_size")



                var itemSizeArray = ArrayList<ItemSize>()
                var itemString = ""
                   for (i in 0 until getArray.length()) {
                       val objects: JSONObject
                       objects = getArray.getJSONObject(i)

                       val name = objects["name"].toString()
                       val value = objects["value"].toString()

                       itemSizeArray.add(ItemSize(name, value))


                       itemSizeArray.forEach { data ->
                           itemString = itemString.plus("${data.name} : ${data.value}")

                       }
                   }

                itemsizename.text = itemString

                Log.v("PRINT", itemsizename.toString())

            }

I want the output as --> Height: 29cm / width:52cm and so on if there are more values added in the "item_size"array.如果“item_size”数组中添加了更多值,我希望 output 为 --> 高度:29cm / 宽度:52cm 等等。 How can that be achieved in this for loop.如何在这个 for 循环中实现。 I want to keep appending all the values of "name" and "value" in a variable(parameter) in the for loop and use this variable outside of the for loop.我想在 for 循环中的一个变量(参数)中继续附加“name”和“value”的所有值,并在 for 循环之外使用这个变量。

Here you just need to create a custom data model class with the name and value variables.在这里,您只需要使用名称和值变量创建自定义数据 model class。

data class ItemSizeDataModel(
val name: String?,
val value: String?
)

Now in the class where you are fetching the JSON make use of the ItemSizeDataModel to store the itemsize - name and value.现在在 class 中,您正在获取 JSON 使用 ItemSizeDataModel 来存储项目大小 - 名称和值。

val json = String(responseBody, charset)
val obj = JSONObject(json)
val getObject = obj.getJSONObject("result")
val getArray = getObject.getJSONArray("item_size")
var parameter: String

var itemSizeArray = ArrayList<ItemSizeDataModel>()

for (i in 0 until getArray.length()) {
        val objects: JSONObject
        objects = getArray.getJSONObject(i)
        val name = objects["name"].toString()
        val Value = objects["value"].toString()
        itemSizeArray.add(ItemSizeDataModel(name = name, value = value))
      }

Now you can access the itemSizeArray array list and access the name and value for each index.现在您可以访问 itemSizeArray 数组列表并访问每个索引的名称和值。

For the output Height: 29cm / width:52cm you can iterate through the loop对于 output 高度:29 厘米/宽度:52 厘米,您可以遍历循环

var itemString = ""
itemSizeArray.forEach{data->
itemString = itemString.plus("${data.name}:${data.value}/")
}
itemString = itemString.dropLast(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM