我想从下面的响应中创建一个数组列表。我的列表应该是类型为Class Temp。

huangapple 未分类评论48阅读模式
英文:

I want create a array List from the response below.My list should be of type Class Temp

问题

{
    "project_id": 1,
    "project_name": "Test Project R",
    "RFI": {
        "OPEN": 0,
        "CLOSE": 0,
        "OVERDUE": 1
    },
    "OBSERVATION": {
        "OPEN": 0,
        "CLOSE": 0,
        "OVERDUE": 0
    },
    "SUBMITTALS": {
        "OPEN": 0,
        "CLOSE": 0,
        "OVERDUE": 1
    },
    "SAFETY": {
        "OPEN": 0,
        "CLOSE": 0,
        "OVERDUE": 0
    },
    "INSPECTION": {
        "OPEN": 0,
        "CLOSE": 0,
        "OVERDUE": 1
    },
    "TASK": {
        "OPEN": 0,
        "CLOSE": 0,
        "OVERDUE": 0
    },
    "WORKFLOW": {
        "OPEN": 0,
        "CLOSE": 4,
        "OVERDUE": 6
    }
}
data class Temp(
    var key: String,
    var open: Int,
    var close: Int,
    var overDue: Int
)
// Your iteration logic to create the desired list

val response = /* ... */ // The JSON response you provided

val tempList = mutableListOf<Temp>()

// Assuming the keys you want to iterate through are dynamic
val dynamicKeys = listOf("RFI", "OBSERVATION", "SUBMITTALS", "SAFETY", "INSPECTION", "TASK", "WORKFLOW")

for (key in dynamicKeys) {
    val category = response[key] as? Map<*, *>
    val open = category?.get("OPEN") as? Int ?: 0
    val close = category?.get("CLOSE") as? Int ?: 0
    val overDue = category?.get("OVERDUE") as? Int ?: 0

    tempList.add(Temp(key, open, close, overDue))
}

Note: Please make sure to adapt the code to your actual programming environment and replace response with the actual JSON response object you have.

英文:

In the key filed of TempClass, I want to add value like "RFI" and the respective open, close, overdue values of it on the remaining fields. How do I iterate through the response to create my desired list? The field names in the response like"RFI", "OBSERVATION", "SUBMITTALS", "SAFETY", "INSPECTION", "TASK", "WORKFLOW" are dynamic and can change in the future.The rest of the response remains the same. Please help.Response:

     {
                &quot;project_id&quot;: 1,
                &quot;project_name&quot;: &quot;Test Project R&quot;,
                &quot;RFI&quot;: {
                    &quot;OPEN&quot;: 0,
                    &quot;CLOSE&quot;: 0,
                    &quot;OVERDUE&quot;: 1
                },
                &quot;OBSERVATION&quot;: {
                    &quot;OPEN&quot;: 0,
                    &quot;CLOSE&quot;: 0,
                    &quot;OVERDUE&quot;: 0
                },
                &quot;SUBMITTALS&quot;: {
                    &quot;OPEN&quot;: 0,
                    &quot;CLOSE&quot;: 0,
                    &quot;OVERDUE&quot;: 1
                },
                &quot;SAFETY&quot;: {
                    &quot;OPEN&quot;: 0,
                    &quot;CLOSE&quot;: 0,
                    &quot;OVERDUE&quot;: 0
                },
                &quot;INSPECTION&quot;: {
                    &quot;OPEN&quot;: 0,
                    &quot;CLOSE&quot;: 0,
                    &quot;OVERDUE&quot;: 1
                },
                &quot;TASK&quot;: {
                    &quot;OPEN&quot;: 0,
                    &quot;CLOSE&quot;: 0,
                    &quot;OVERDUE&quot;: 0
                },
                &quot;WORKFLOW&quot;: {
                    &quot;OPEN&quot;: 0,
                    &quot;CLOSE&quot;: 4,
                    &quot;OVERDUE&quot;: 6
                }
            }

data class Temp(
        var key: String,
        var open: Int,
        var close: Int,
        var overDue: Int
)

huangapple
  • 本文由 发表于 2020年5月5日 19:42:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/61612300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定