java - 如何访问数组中自动生成的java对象的数据?
问题描述
我正在尝试从 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]),但是当我想访问对象的数据时,比如 object Magasin @34a245abSystem.out.println(objMag.get(2).getVille);
我总是从对象 Magasin@1d81eb93 获取数据。如何访问其他对象?我的对象生成错误吗?
解决方案
使用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") 从该列表中获取并放入所需的对象
推荐阅读
- c# - C#:REST url 返回 404,而相同的 URL 在 Web 浏览器中有效
- php - 为什么 php-fpm 无法启动?
- airflow - 是否可以从 SubDag 访问 dag_run.conf?
- r - 在 R 中安装 Broom 和 Nlme 包时出错
- vhdl - 用两个输入驱动一个输出 vhdl
- node.js - 关于nodeJS的澄清
- python - Word & Line Concordance Program
- javascript - Javascript - 使用 null 作为默认参数
- bash - 在mac上匹配sed中的重复字符
- r - 错误:查询所有 ZCTA5 的数据时未知/不支持的地理层次结构