Deserialize JSON to Object in Java

0

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
source

1 answer

0

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{
    @SerialName("latitude")
    private double latitud;
    @SerialName("longitude")
    private double longitud;
    @SerialName("name")
    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