[ad_1]
I got this json response
{
"status": [
{
"conferencing": {
"nrOfRxBytes": 1142294,
"nrOfTxBytes": 368502,
"CategoryActiveTime": 9625,
"Active": true
},
"social_media": {
"nrOfRxBytes": 163156131,
"nrOfTxBytes": 1438337,
"CategoryActiveTime": 10406,
"Active": true
},
"media_streaming": {
"nrOfRxBytes": 5065342,
"nrOfTxBytes": 312250,
"CategoryActiveTime": 213,
"Active": false
},
"Device": "BE:A0:FE:74:39:C1"
},
{
"conferencing": {
"nrOfRxBytes": 58534,
"nrOfTxBytes": 24615,
"CategoryActiveTime": 10343,
"Active": true
},
"social_media": {
"nrOfRxBytes": 5618,
"nrOfTxBytes": 2731,
"CategoryActiveTime": 14308,
"Active": true
},
"media_streaming": {
"nrOfRxBytes": 747,
"nrOfTxBytes": 397,
"CategoryActiveTime": 448,
"Active": false
},
"education": {
"nrOfRxBytes": 404,
"nrOfTxBytes": 260,
"CategoryActiveTime": 419,
"Active": false
},
"Device": "AC:12:03:EC:9B:D1"
}
]
}
i want to join similar catgories (education, conferancing…) together and aggregate the values of similiar fields and make a list of “Device” that are in that category
like this:
[{
"name":"conferencing":
"nrOfRxBytes": 1200828, // agg this
"nrOfTxBytes": 393 117, // agg this
"CategoryActiveTime": 19 968, // agg this
"Active": true,
"Device": ["AC:12:03:EC:9B:D1","BE:A0:FE:74:39:C1"] //this is a list of devices that are in the same category
},...]
I tried creating parcelized data class
@Parcelize
data class ServicesIdCategory(
var name: String = "",
val nrOfRxBytes: Int = 0,
val nrOfTxBytes: Int = 0,
val CategoryActiveTime: Int = 0,
val Active: Boolean = false,
var Device: String = ""
) :
Parcelable
and then i parsed the the results to it and created a list of ServicesIdCategory
fun getLiveServicesId(response: JSONArray): Array<ServicesIdCategory> {
val array = mutableListOf<DeviceServicesId>()
var categories = mutableListOf<ServicesIdCategory>()
var category = ServicesIdCategory()
val gson = Gson()
for (i in 0 until response.length()) {
val obj = response.getJSONObject(i)
val namesArr = obj.names()
if (namesArr != null) {
for (j in 0 until namesArr.length()) {
if (namesArr[j].toString() != "Device") {
val categoryJson = obj.optString(
namesArr[j].toString(), ""
)
try {
category = gson.fromJson(categoryJson, ServicesIdCategory::class.java)
category.name = namesArr[j].toString()
} catch (e: JsonSyntaxException) {
}
}
category.Device = obj.optString("Device")
categories.add(category)
}
}
val deviceServiceId = gson.fromJson(obj.toString(), DeviceServicesId::class.java)
deviceServiceId.servicesIdCategory = categories.toTypedArray()
array.add(deviceServiceId)
}
return categories.toTypedArray()
}
[ad_2]