我有以下java代碼,我試圖從JSONArray物件中提取一組整數。我該怎么做?
JSONObject actionDetail = new JSONObject("myJsonOject");
int personId = actionDetail.getInt("personId");
JSONArray addressIds = actionDetail.getJSONArray("addressIds");
Action action = new Action();
action.setPersonId(personId); //working ok
action.setAddressIds(): //todo - how to get list of ints from the JsonArray?
注意addressIds欄位的型別是:Set<Integer>
uj5u.com熱心網友回復:
您可以嘗試在流中將 Object 轉換為 Integer。
action.setAddressIds(addressIds.toList().stream().map(k -> (Integer) k).collect(Collectors.toSet()));
uj5u.com熱心網友回復:
您可以嘗試以下操作:
Set<Integer> result = IntStream.range(0, addressIds.length())
.mapToObj(addressIds::get)
.map(Integer::valueOf)
.collect(Collectors.toSet());
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/388865.html
