I need to create an array with data that I have in 4 SQLite tables, these 4 tables have their 4 entities with their Getter and Setter methods, the question is to create a JSON using the GSON libraries, I would have to create an entity with the fields of the 4 classes ? since I need the data from all 4 tables to go as a single row.
String JSON = gson.toJson(detalle1);
String JSON = gson.toJson(detalle2);
String JSON = gson.toJson(detalle3);
String JSON = gson.toJson(detalle4);
With the above I create a JSON for each entity but I need, for example, that the first row of each entity go in a single array since they are dependent fields when saving them in the database.
I recently started working with GSON and somewhat complex data models, with related classes.
The truth is that at first I wasn't very clear about how to do things, what helped me was to fully safeguard the data model as I had designed it in each table and build my JSON according to that model, respecting the reality of the data .
Allow me to present a model that is a bit long and complex, but it can help to deal with the problem in a complete way, since it includes data nested at more than one level at many points.
The JSON that is produced is as follows:
To make it short, let's take some examples of this JSON.
This part of the JSON represents
santo
the database table, which has two columns:nombre, vida
:The class is like this:
Then you can see a
visperas
much more complex node with several primitive elements (string, int, etc) and other elements that are of another class . If you look at the node, you can deduce that theyhimno, salmodia, lecturaBreve, magnificat y preces
are independent classes that in turn are part ofvisperas
, while itoracion
is a let's say primitive element (string) that is also part of eves.The class
visperas
is therefore presented as follows:If you want to continue analyzing, you can see that inside the node
salmodia
there is still another node that is a class of its own, it is called:salmoCompleto
.The class
Salmodia
looks like this:You will notice that it
salmocompleto
is defined inSalmodia
as a list of the typeSalmoCompleto
:List<SalmoCompleto>
The class
SalmoCompleto
is defined like this:This is very interesting and very useful. Because, for example, I need each psalm, be it 3, be it 50, to bring a specific format. To say something, that the letter color of the member
tema
is red. Well, I do all that complete work in the methodgetSalmoCompleto
of the classSalmodia
, which goes through the array of psalms and formatting.get references
I show an example how I get each reference from the different nodes of my JSON:
For example, a process that could be complicated, such as obtaining the entire psalm with different formats, colors, and font sizes, becomes as simple as doing this:
the method does the rest.
Have clear criteria
At first I was a bit lost with GSON. My criteria was: build a JSON that respects my data model. In other words, if I have a table
himno
, the JSON must have a node calledhimno
, if I have a tablevisperas
, which in turn uses elements from other tables, my JSON must have a nodevisperas
and the sub-nodes that are needed.From there we can deduce that the most important thing is to structure your JSON well, respecting your data model . It would have been apparently easier not to create a node
salmodia
in addition with a sub-nodesalmoCompleto
and put the array of psalms directly into the main node. But it turns out that there are other tables that also usesalmodia
, tables similar tovisperas
, but with another function. If I (apparently) easily remove asalmoCompleto
from the nodesalmodia
and put it directly in the nodevisperas
, I do nothing but disrespect the data design (a Complete psalm is a Complete psalm), and I also complicate my life when I have to format each element of the psalm or when you have to use it from another of the classes that also use elements of typeSalmoCompleto
.A very useful tool
There is a tool that helped me a lot to implement the data model correctly. Once you have the JSON built according to your data model, you can paste it and it will generate the class correctly. It is
jsonutils
, an online tool that generates classes from JSON. The only criticism I can make is that it creates the members of the class aspublic
, in my case I change them toprivate
to make the management of the members go through the getter and the setter .P.S:
I hope this answer will help you for your data model. If there is any doubt you can ask in comments.