3
I was wondering if there is a framework to read the data that comes in the json of google maps Directions? type, take the data and put in variables that I can take and use in the rest of the application
3
I was wondering if there is a framework to read the data that comes in the json of google maps Directions? type, take the data and put in variables that I can take and use in the rest of the application
4
You can use the Google Gson.
With it, you define a class and it populates all attributes for you. For example:
public class Pessoa {
private String nome;
private int idade;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
}
Considering the JSON:
{
"nome":"Joao",
"idade":"25"
}
So you can do:
Gson gson = new Gson();
Pessoa pessoaDoJson = gson.fromJson(jsonString, Pessoa.class);
And Gson will transfer the JSON values to the object attributes pessoaDoJson
.
If you want, you can do the reverse path as well: given a populated object, create a JSON:
Pessoa p = new Pessoa();
p.setNome("Joao");
p.setIdade(25);
String json = gson.toJson(p);
//o valor da variável json é: { "nome":"Joao", "idade":"25" }
EDIT:
For ease, you can use something like the jsonSchema2Pojo to build your JSON-based POJO.
But good friend, Gson solves even. Or try Jackson. Another framework with the same result.
@Guilhermecostamilam I can’t see JSON.. Gives access error.. Giant in what sense? Many very large attributes or attributes?
giant in both directions, are various object array with other objects and object arrays, if I were to do all classes would give more than 20 for sure
Need to have direct control of JSON? If not you can use that library. DirectionsResult result = DirectionsApi.getDirections(sc.context, "Sydney, AU", "Melbourne, AU").await();
already gives you the results with POJOS.
can reformulate the response with an example of use, preferably with the &mode=transit
?
@Igorventurelli, add the JSON class creator that I put as a comment on the question in your answer, it may be?
@Gustavocinque you say the jsonschma2pojo? It is for me to add in the answer?
Yes. That’s the one.
@Gustavocinque beauty already change the answer
@Gustavocinque edited response ;)
Browser other questions tagged java android google-maps
You are not signed in. Login or sign up in order to post.
Gson. Google it.
– Gustavo Cinque
Can show an example of use?
– Costamilam
@Costamilam, you will have to create a class to represent each json item you showed Venturelli. A class
GeocodedWaipoint
, a classRoute
, a classBound
, oneLeg
, and many more. Each attribute that contains internal attributes will need to be a class. With this it is necessary to follow the steps described by Venturelli himself.– Gustavo Cinque
Looks like I found something make life easier for everyone. Just paste the JSON, modify the options and press Preview.
– Gustavo Cinque
Truth @Gustavo Cinque, I forgot that there was this class generator, thanks for the help, put in an answer and I accept your answer, the problem is that now I have created the 23 classes but next I will try to remember
– Costamilam
You can accept @Igorventurelli’s friend, I ask him to include in the answer below.
– Gustavo Cinque