Manipulating a JSON with the GSON library

Asked

Viewed 807 times

2

This is the json:

{
"professor": [
{
  "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"
}
],
"medico": [
{
  "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"
}
]
}

I’d like to manipulate you, which would be to read the json, and record more data.

For example a new profession and a new place, being able to register several places in a profession. That is to say, I would like to transform this json in object with gson!

  • You own a class Profissoes, or several as: Professor, Medico, etc? See if that one answer help you.

  • I don’t own these classes, it would be dynamic!

  • there is the possibility of being created?

  • I had seen that Voce told me to take a look, I’ll try to make an example to see

  • You have to create the necessary class to deserialize your JSON, I will try to explain you in a reply.

  • Thank you @Zulian, I’m waiting

Show 1 more comment

1 answer

2


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
  • The code this super functional I am able to read perfectly! and if I want to register more data inside this json how can I do? Could you help me?

  • You need to use lists to make it easier. I updated the response.

  • beauty, when I put the code he told me to import the type, however appeared several type options have some that would be correct?

  • Got here, another and last doubt if I register a site I would do how ? <3 worth nois

  • Create a location like this: Local local = new Local();. Seven attributes required. Ex.: local.setTitle("Teste");. Then add to the array of profession locations, doing something like: profissaoNova.getLocal().add(local);.

  • 1

    Man you are a GOD! thank you very much saved my skin in college ! <3

Show 1 more comment

Browser other questions tagged

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