gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:49:20 25 4
gpt4 key购买 nike

我正在尝试从 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]),但是当我想访问对象的数据时,像对象 Magasin@34a245ab 和 System.out.println(objMag.get(2).getVille);我总是从对象 Magasin@1d81eb93 获取数据。我如何访问其他对象?我的对象生成错误吗?

最佳答案

使用org.apache.commons.io.IOUtilscom.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") 从该列表中获取并放入所需的对象

关于java - 如何访问数组中自动生成的java对象中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54226300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com