Convert java object to map

1

I have an object that has about 60 attributes that are both of type String and of type double, plus a few that are of other types, and by chance I need a map where the keys are the names of the attributes of the object and value its values, however I also want to be able to choose which attributes of that object should not be converted to map, that is, I only want those that are of type String and double to be mapped.

    
asked by gibran alexis moreno zuñiga 14.03.2017 в 21:57
source

2 answers

1

I solved it using Jackson with the following:

ObjectMapper mapper = new ObjectMapper();

    Map<String, Object> atributosGuiaFedex = 
        mapper.convertValue(guiaFedex, Map.class);
    System.out.println(atributosGuiaFedex);

Where guiaFedex is the object that I want to convert to map and to ignore the attributes I placed the following up to the class I wanted to convert:

@JsonIgnoreProperties({"guia","periodo","id","fechaEnvio","cargosDeGuiaAerea"})

where what is in quotation marks are the attributes to be ignored.

    
answered by 14.03.2017 / 22:48
source
0

what you can do is convert your class into Iterator to be able to iterate over its attributes, when you iterate you must condition if it is string o double

if (atributo instanceof String) {
  esCadena = (String) atributo;
}
    
answered by 14.03.2017 в 22:44