如何从数组中自动生成的Java对象访问数据?

时间:2019-01-16 22:22:23

标签: java arrays json arraylist

我正在尝试根据REST API生成的JSON内容自动创建Java对象。有问题的“东西”是一个包含商店属性的JSON数组,我想按商店生成一个对象,每个对象都包含JSON数组中每个商店的属性。

我使用该代码实现了这一目标:

                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);

            }

假设我们拥有(长)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"
  }
]

如果我打印objMag数组,则应该生成4个对象([Magasin @ 1d81eb93,Magasin @ 7291c18f,Magasin @ 34a245ab,Magasin @ 7cc355be]),但是当我要访问对象的数据时,就像对象System.out.println(objMag.get(2).getVille);的对象Magasin @ 34a245ab 我总是从对象Magasin @ 1d81eb93获取数据。 如何访问其他对象? 我的对象生成错误吗?

1 个答案:

答案 0 :(得分:1)

使用 org.apache.commons.io.IOUtils com.fasterxml.jackson.databind.ObjectMapper 会立即解决您的问题。

在下面使用:

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;
}

和提取的函数。

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;

请记住,根据json的结构,ObjectMapper可能会返回一个包含键值对的LinkedHashMap列表。因此,您最终可能会使用该列表中的map.get(“ key”)进行提取并将其放入所需的对象