Deserialize JSON to Object in Java


How can I deserialize the attribute properties of Object -> templateComponents to be able to access only the information of latitude, longitude y name ?

Right now properties is a String and I need it to be a Object .

asked by charli 08.11.2018 в 14:44

1 answer


That properties can be converted to a Java object using the Gson library.

You need a class that models that object and then Gson takes care of the deserialization.

Unfortunately you hit an image instead of putting text, so the example looks only at the first three attributes.

public class MisPropiedades{
    private double latitud;
    private double longitud;
    private String nombre;

    // Aca getters y setters

Then in the program:

String props = templateComponenets.get(4).getProperties(); // Supongo que esto devuelve el string properties
MisPropiedades misPropiedades = new Gson().fromJson(props, MisPropiedades.class);

And in misPropiedades you have instantiated the object.

Here you can get the library either the jar or how to include it in maven.

answered by 08.11.2018 в 15:19