英文:
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:
{
"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
)
专注分享java语言的经验与见解,让所有开发者获益!
评论