Manipulating a JSON with the GSON library

2

This is 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 would like to manipulate it, which would read json , and write more data.

For example a new profession and a new place, being able to register several places in a profession. That is, I'd like to make this json into object with gson !

    
asked by anonymous 30.10.2017 / 13:09

1 answer

2

As you have professions and locations in your JSON, you need to have these classes with those same attributes in order to make this JSON a manipulable object.

The basic structure in your case is: a Professions array that has a Places array . This JSON requires there changes, since it has an array of Locais but not one of professions, since it lacks a key ([) at the beginning and at the end of everything. In addition you need to enter an attribute for the job description. First you need to address this so you do 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:

Profession:

public class Profissao {
  String descricao;
  ArrayList<Local> local;

  public Profissao() {  //construtor
    this.descricao = "";
    this.local = new ArrayList();
  }
  // setters e getters aqui
}

Location:

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 deserialize your JSON is easy, use the gson.fromJson method by passing its json and 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 attribute names should be the same as JSON's for Gson to pick up automatically, if you want to use other names, use Gson's own annotations.

PS: There's a cool site that generates classes based on your JSON!

Using List

To work with dynamic arrays, such as List for example, you need to use TypeToken " from Gson to convert your json to 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
    
30.10.2017 / 14:09