Issue
I have JSON response like this:
{
"1": "string",
"2": "string",
"3": "string",
"4": "string",
"collection": [
{
"col_1_1": "string",
"col_1_2": 37,
"col_1_3": "string",
"col_1_4": "string",
},
{
"col_2_1": "string",
"col_2_2": 37,
"col_2_3": "string",
"col_2_4": "string",
}
]
}
How can I achieve result like this:
{
{
"1": "string",
"2": "string",
"3": "string",
"4": "string",
"col_1_1": "string",
"col_1_2": 37,
"col_1_3": "string",
"col_1_4": "string",
},
{
"1": "string",
"2": "string",
"3": "string",
"4": "string",
"col_2_1": "string",
"col_2_2": 37,
"col_2_3": "string",
"col_2_4": "string",
}
}
It is possible to make it without external libraries ? How can I achieve it ? Has anyone ever dealt with such an issue?
POJOS:
public class MainDTO {
private String param1;
private String param2;
private String param3;
private String param4;
private Set<CollectionDTO> collection;
}
class CollectionDTO {
private String param1;
private String param2;
private String param3;
private String param4;
}
Class is something like that. I have tried in many ways to no avail. Generally, the mapper's final method should only take one parameter in the form MainDTO
maptoDto(MainDTO dto) {...}
I made a mapping method that takes MainDTO and CollectionDTO, but further inside the method I think I need to do another mapping as well so that each element is extracted: /
Solution
OK i have solution. I have created one DTO with parameters that are needed, which are in both entities, the main entity and the entity that is the collection. Then the method that creates the dto object, where it gives both classes in parameters:
private fun createDTO(dto: MainEntity, collection: CollectionEntity): MyDTO =
MyDto(
//...parameter assignment
)
and mapping function (kotlin):
internal fun toDTO(dto: MainEntity): Set<MyDTO > {
return dto.collestion
.map { createDTO(dto, it) }
.toSet()
}
It's working ;) Thanks for sugestions
Answered By - mat373
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.