1

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 ?

Awad
  • 823
  • 3
  • 11
  • 33
Tcherno
  • 11
  • 3
  • You want to map a JSON Objects list into a list of java objects? Try jackson library, you can find an example here: https://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects – Manuel Espinosa Jan 16 '19 at 22:30

1 Answers1

1

Using org.apache.commons.io.IOUtils and com.fasterxml.jackson.databind.ObjectMapper would solve your problem in no time.

Use below:

class MegaSinList{
    private List<MegaSin> megasinList;
}

class MegaSin{
    private String codePostale;
    private String createur;
    private String creationDate;
    private String creationShop;
    private String description;
    private int identifiant;
    private String modification;
    private String nom;
    private int nombreEmploye;
    private String rue;
    private String statutLegal;
    private long turnOverRange1;
    private long turnOverRange2;
    private String type;
    private String ville;
}

and the finction to extract..

byte[] jsonData = IOUtils.toByteArray("/...filepath../file.json").getInputStream());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
MegaSinList magasinListObj = objectMapper.readValue(jsonData, MegaSinList.class);
return magasinListObj;

Remember, depending upon the structure of json, ObjectMapper may return a list of LinkedHashMap containing key-value pairs. So you might end up fetching using map.get("key") from that list and put into the desired object

Kumar Ashutosh
  • 1,121
  • 10
  • 33
  • Do I have to install these or are they include by default in Eclipse and and/or Android studio ? (because I'll have to implement that in an Android app when it will work) I use org.json because it was included and seemed pretty simple to use (and the rest of the app json treatment is done with it) – Tcherno Jan 16 '19 at 22:45
  • No, you have to include the relevant jars for these like "commons-io-2.6" fro apache commons and "jackson-databind" jars. Than after you can import the packages into your classes – Kumar Ashutosh Jan 16 '19 at 22:48
  • If you are using maven, use commons-io commons-io 2.6 and com.fasterxml.jackson.core jackson-databind 2.9.7 . Or you can refer https://mvnrepository.com/ , type the words and you get the dependency and the jars to download. Just include them in classpath – Kumar Ashutosh Jan 16 '19 at 22:52
  • Does all of these libraries compatible with each others ? As I made a big part of the app with org.json, (for API requests), I don't want that mixing stuff break all my work... – Tcherno Jan 16 '19 at 22:58
  • yes apache commons 2.6 and jackson-databind 2.9.7 are compatible. You might need to validate others if they are conflicting. Although, you bare good to go with this. – Kumar Ashutosh Jan 16 '19 at 23:01