如何合并列表中的类似项目并汇总Kotlin中的值
我得到了这个JSON响应
{
"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"
}
]
}
我想加入类似的猫(教育,会议...)并汇总类似字段的值,并列出这样的“设备”列表
:
[{
"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
},...]
我尝试创建包裹数据类别
@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
然后,我将结果解析到其中,并创建了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()
}
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()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论