Since you have professions and locations in your JSON, you need to have these classes with these same attributes in order to transform this JSON into a manipulable object.
The basic structure in this case is: a array of professions who owns a array of Locations. This JSON there needs changes, as it has an array of Locais
but not one of professions, as a key ( [ ) is missing at the beginning and end of everything. In addition you need to enter an attribute for the description of the profession. First you need to treat this to not have a class of each profession. Ex correct:
[
{
"descricao": "Professor",
"local": [
{
"latitude": -23.1843473,
"longitude": -45.8840718,
"title": "Microcamp",
"endereco": "rua vilaça 2010"
},
{
"latitude": -23.1843473,
"longitude": -45.8840718,
"title": "Microcamp",
"endereco": "rua vilaça 2010"
}
]
},
{
"descricao": "Médico",
"local": [
{
"latitude": -23.1843473,
"longitude": -45.8840718,
"title": "Microcamp",
"endereco": "rua vilaça 2010"
},
{
"latitude": -23.1843473,
"longitude": -45.8840718,
"title": "Microcamp",
"endereco": "rua vilaça 2010"
}
]
}
]
Then just create the classes:
Professional:
public class Profissao {
String descricao;
ArrayList<Local> local;
public Profissao() { //construtor
this.descricao = "";
this.local = new ArrayList();
}
// setters e getters aqui
}
Local:
public class Local {
Double latitute;
Double longitude;
String title;
String endereco;
public Local() { //construtor
this.latitute = 0.00;
this.longitude = 0.00;
this.title = "";
this.endereco = "";
}
// setters e getters aqui
}
Once you have these classes, to desirialize your JSON is easy, use the method gson.fromJson
passing his json
and the class to be used. Ex:
Profissao[] profissoes = gson.fromJson(json, Profissao[].class);
If your JSON is correct, you will have an array of professions with their respective locations.
Note that the attributes name must be the same as the JSON for Gson to pick up automatically, if you want to use other names, use Gson’s own annotations.
PS.: There is a really cool website that generates classes based on your JSON!
Using List
To work with dynamic arrays, such as List
for example, you need to use the TypeToken
from Gson to convert your json into a List
. Then it becomes easy to add more objects and transform to json again. Ex.:
Type type = new TypeToken<List<Profissao>>(){}.getType();
List<Profissao> profissoes = gson.fromJson(json, type); //converte o json para uma lista
Profissao profissaoNova = new Profissao(); //cria uma nova profissao
profissaoNova.setDescricao("Programador");
profissoes.add(profissaoNova); //adiciona a profissao no array
String jsonString = gson.toJson(profissoes); //transforma para json novamente
You own a class
Profissoes
, or several as:Professor
,Medico
, etc? See if that one answer help you.– Zulian
I don’t own these classes, it would be dynamic!
– Rodrigo Gabriel
there is the possibility of being created?
– Rodrigo Gabriel
I had seen that Voce told me to take a look, I’ll try to make an example to see
– Rodrigo Gabriel
You have to create the necessary class to deserialize your JSON, I will try to explain you in a reply.
– Zulian
Thank you @Zulian, I’m waiting
– Rodrigo Gabriel