读取哈希映射中显示的所有值



我已经定义了HashMap列表并读取JSON API的响应。目前只能从列表中读取一个值,我想读取所有值。

List<HashMap<String,Object>> allids = response.jsonPath().getList("data");

HashMap<String,Object> firstid = allids.get(0);  
Object a =  firstid.get("country");
System.out.println(a);

邮递员中的 JSON 响应

{
"response": {
"code": 200,
"status": "success",
"alert": [
{
"message": "Success",
"type": "success",
"skippable": 1
}
],
"from_cache": 0,
"is_data": 1
},
"data": [
{
"id": 6004,
"airport_name": "Adampur Airport",
"city": "Adampur",
"country": "India",
"iata": "AIP",
"icao": "VIAX",
"latitude": "31.4338",
"longitude": "75.758797",
"altitude": "775"
}
]
}

只需forEach你的List你就会得到Map,然后使用get获取所有Objectbu 。

List<HashMap<String,Object>> allids = response.jsonPath().getList("data");
allids.forEach(elem->{
String country = (String) elem.get("country");
String city = (String) elem.get("city");
// and so on.
});

从上下文来看,您可以遍历列表并获取 Map 值

List<HashMap<String,Object>> allids = response.jsonPath().getList("data");
for(int i=0; i<allids.size(); i++){
HashMap<String,Object> firstid = allids.get(i);  
String country = (String) firstid.get("country");
String city = (String) firstid.get("city");
String iata = (String) firstid.get("iata");
String altitude = (String) firstid.get("altitude");
//similarly get others
System.out.println(country);
}

相关内容

  • 没有找到相关文章

最新更新