I'm trying to create java objects automatically from a JSON stuff that is generated from a REST API. The "stuff" in question is a JSON array containing shop properties, and I want to generate one object by shop, each one containing the properties of each shop in the JSON array.
I achieved that goal with that code :
ArrayList<Magasin> objMag= new ArrayList<Magasin>();
JSONArray databrute = new JSONArray(sortie);
for (int i = 0; i < databrute.length(); i++){
ville = databrute.getJSONObject(i).getString("ville");
nom = databrute.getJSONObject(i).getString("nom");
objMag.add(new Magasin(ville,nom/*,rue,type,descrp,createur,statutlegal,datecreat,datemodif,magcreat,codepost,id,nbemployes,turnover1,turnover2*/));
}
System.out.println(objMag.get(2).getVille);
}
Assuming we have that (long) JSON :
[
{
"identifiant": 1,
"nom": "A Sollicitudin Orci Corporation",
"description": null,
"type": "eu",
"rue": "Ap #654-2176 In Street",
"codePostale": 18376,
"ville": "Agra",
"creationShop": "2018-06-08T00:00:00",
"createur": null,
"statutLegal": null,
"nombreEmploye": 589,
"turnOverRange1": 1,
"turnOverRange2": 60000,
"creationDate": "2018-12-16T00:00:00",
"modification": "2018-07-07T00:00:00"
},
{
"identifiant": 2,
"nom": "Non Company",
"description": null,
"type": "sollicitudin a,",
"rue": "P.O. Box 293, 3347 Posuere, Av.",
"codePostale": 45680,
"ville": "Ligny",
"creationShop": "2018-05-28T00:00:00",
"createur": null,
"statutLegal": null,
"nombreEmploye": 234,
"turnOverRange1": 1,
"turnOverRange2": 60000,
"creationDate": "2018-08-24T00:00:00",
"modification": "2018-05-05T00:00:00"
},
{
"identifiant": 3,
"nom": "Orci Incorporated",
"description": null,
"type": "at,",
"rue": "Ap #199-3370 Sit St.",
"codePostale": 71636,
"ville": "Seraing",
"creationShop": "2018-04-17T00:00:00",
"createur": null,
"statutLegal": null,
"nombreEmploye": 198,
"turnOverRange1": 1,
"turnOverRange2": 60000,
"creationDate": "2018-02-09T00:00:00",
"modification": "2017-12-12T00:00:00"
},
{
"identifiant": 4,
"nom": "Metus PC",
"description": null,
"type": "quis",
"rue": "Ap #144-9271 Enim St.",
"codePostale": 87755,
"ville": "Bangor",
"creationShop": "2019-08-24T00:00:00",
"createur": null,
"statutLegal": null,
"nombreEmploye": 530,
"turnOverRange1": 1,
"turnOverRange2": 60000,
"creationDate": "2018-06-27T00:00:00",
"modification": "2018-06-06T00:00:00"
}
]
I should have 4 objects generated, (which are [Magasin@1d81eb93, Magasin@7291c18f, Magasin@34a245ab, Magasin@7cc355be]) if I print the objMag array, but when I want to access the data of an object, like object Magasin@34a245ab with System.out.println(objMag.get(2).getVille);
I always obtain the data from object Magasin@1d81eb93.
How can I access the others objects ?
Is my object generation wrong ?