Framework to read json google maps api

3

I would like to know if there is any framework to read the data that comes in json from google maps directions? type, get the data and put in variables that I can pick up and use in the rest of the application

    
asked by anonymous 10.11.2017 / 15:14

1 answer

4

You can use Google Gson .

With it, you define a class and it populates all the 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 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 attributes of the pessoaDoJson object.

If you want, you can do the inverse path too: 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:

To make it easier, you can use something like jsonSchema2Pojo to mount your POJO based on JSON.

    
10.11.2017 / 16:37