Hey, I have a JSON response and i need to filter that data in a list. In every JSONObject there are 3 categories in which only one category holds data.
data class CategoryResponse(
val item: List<CategoryItem>
)
data class CategoryItem(
val id: Int,
val Category1: List<ItemOptions>? = null,
val Category2: List<ItemOptions>? = null,
val Category3: List<ItemOptions>? = null
)
data class ItemOptions(
val name: String? = null,
val url: String? = null
)
I have an enum class, which has 4 enum types
enum class CategoryType(val name :String){
ALL("ALL"),
Category1("Category1"),
Category2("Category2"),
Category3("Category3");
}
There is a category named ALL, so i need the list of all the data but I don’t want any null values, and still want the data that other categories hold.
Example - the list consist of 5 elements
Desired output:
ALL : list-> [id : 1 , category1 : ”…”] , [id : 2 ,category2 : “…”] , [id : 3 ,category2 : ”…”] , [id : 4 ,category1 : “…”] , [id : 5 ,category3: “…"]
CATEGORY2: list → [id : 2 , category2 : “…”] , [id : 3 , category2 : “…”]
(this list may or may not consist of more than 5 elements)
is there any way that this list won’t occupy a lot of memory, or duplicate the objects and could be more efficient.?
{
"items": [{
"id": "1",
"Category1": [{
"name": "Item ..",
"url": "https:..."
}, {
"name": "Item ..",
"url": "https:..."
}],
"Category2": [],
"Category3": []
}, {
"id": "2",
"Category1": [],
"Category2": [{
"name": "Item ..",
"url": "https:..."
}],
"Category3": []
}, {
"id": "3",
"Category1": [],
"Category2": [{
"name": "Item ..",
"url": "https:..."
}],
"Category3": []
}, {
"id": "4",
"Category1": [{
"name": "Item ..",
"url": "https:..."
}],
"Category2": [],
"Category3": []
},{
"id": "5",
"Category1": [],
"Category2": [],
"Category3": [{
"name": "Item ..",
"url": "https:..."
}]
} ....]
}