Object in java contains a JSON, I want to take the JSON information and popular my object attributes

Asked

Viewed 246 times

0

The thing is, I have the GPS object, it’s with a json as follows the image, I want to take the data from the code field and the name field and put it inside the attributes of the GPS object. IMMAGE inserir a descrição da imagem aqui

How can I do that ?

I tried that way, but without success

this.nome = lista.get(acessorio).nome;
this.codigo = lista.get(acessorio).codigo;

1 answer

1

There is already a library that does this, populate the objects and convert the object to json.

To popular the object:

Gson gson = new Gson();
Acessorio gps = gson.fromJson(json, Acessorio.class);

Converts the object to json:

Gson gson = new Gson();
String json = gson.toJson(objeto);

The objects must be a Javabeans.

Include dependency Maven to your project or download the Jar’s.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.