Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
How to deserialize list with dynamic number of keys with same value structure?

I am trying to find an optimal way to deserialize dynamic fields that all contain the same value object structure:

"data": {
    "lastUpdate": 1605299294253,
    "tableData": [
        {
            "recordId": 1,
            "isSelected": 1,
            "myKey1": {
                "valueRef": 72,
                "value": "Some value"
            },
            "myKey2": {
                "valueRef": 0,
                "value": "123"
            }
        }
    ]
}

For the tableData JSON array, I have tried this approach without success:

import lombok.Data;
import java.util.Map;

@Data
public class TableRowDataVO {
    private Integer recordId;
    private Integer recordStatus;
    private Map<String, TableRowDetailVO> columnData1;
    private Map<String, TableRowDetailVO> columnData2;
}

This class is nested into another class to encompass the entire JSON body in data:

public class TabularDataDTO {
    private Long lastUpdate;
    private List<TableRowDataVO> tableData;
}

and each dynamic field holds the same Object of the following type:

public class TableRowDetailVO {
    private Integer valueRef;
    private String value;
}

There is also no guarantee that there are only 2 dynamic keys. It can be 0 to many dynamic keys that are in TableRowDataVO, but if there are limitations that require me to settle with 2, I can do that, but if 0 to many dynamic fields are possible, that would be great. I am trying to use the ObjectMapper with TypeReference as TabularDataVO but it throws an exception. What is the proper way to design my objects such that TabularDataVO can be deserialized by the mapper?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

since the key is dynamic so when you process data you need some special process.

@Data
public class NewDTO {
    private Long lastUpdate;
    private List<Map<String, Object>> tableData;
}
over 4 years ago · Santiago Trujillo Relatório

0

Just to understand your question correctly. You Json is like following: -

"data": {
    "lastUpdate": 1605299294253,
    "tableData": [
        {
            "recordId": 1,
            "isSelected": 1,
            "myKey1": {
                "valueRef": 72,
                "value": "Some value"
            },
            "myKey2": {
                "valueRef": 0,
                "value": "123"
            }
        }
    ]
}

Not like

"data": {
    "lastUpdate": 1605299294253,
    "tableData": [
        {
            "recordId": 1,
            "isSelected": 1,
            "keys": [
                 {
                   "valueRef": 72,
                   "value": "Some value"
                },
                {
                  "valueRef": 0,
                  "value": "123"
               }
           ]
        }
    ]
}

Latter can be easily deserialized using jackson library and defining keys as List<Map<String, Object>>

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda