How to access data from java objects automatically generated in array?

问题: 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'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 ?


回答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

  • 发表于 2019-01-18 10:53
  • 阅读 ( 233 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除