英文:
getting or fetching the Value from an Object Array , from an HashMap
问题
我已经得到了以下的映射结构
{CDetails=[{"collegeName":"Peters Stanford","collegeLoc":"UK"},{}]}
我正在尝试从上述提到的结构中获取 collegeLoc 的值
这是我尝试过的
public static void main(String[] args) {
Map<String, Object> empMap = new HashMap<>();
JSONObject collegeNameJsonObj = new JSONObject();
collegeNameJsonObj.put("collegeName", "Peters Stanford");
JSONObject collegeIdJsonObj = new JSONObject();
collegeNameJsonObj.put("collegeLoc", "UK");
JSONArray jsonArray = new JSONArray();
jsonArray.add(collegeNameJsonObj);
jsonArray.add(collegeIdJsonObj);
empMap.put("CDetails", jsonArray);
System.out.println(empMap.entrySet().stream().filter(map->map.getKey().equals("CDetails")).map(map->map.getValue()).collect(Collectors.toList()));
卡在这里,无法进一步循环/解析。
在这种情况下如何获取 collegeLoc 的值?任何帮助都将非常感谢。
英文:
I have got the following Map Structure
{CDetails=[{"collegeName":"Peters Stanford","collegeLoc":"UK"},{}]}
I am trying to get the collegeLoc value from the above mentioned structure
This is what i tried
public static void main(String[] args) {
Map < String, Object > empMap = new HashMap < > ();
JSONObject collegeNameJsonObj = new JSONObject();
collegeNameJsonObj.put("collegeName", "Peters Stanford");
JSONObject collegeIdJsonObj = new JSONObject();
collegeNameJsonObj.put("collegeLoc", "UK");
JSONArray jsonArray = new JSONArray();
jsonArray.add(collegeNameJsonObj);
jsonArray.add(collegeIdJsonObj);
empMap.put("CDetails", jsonArray);
System.out.println(empMap.entrySet().stream().filter(map->map.getKey().equals("CDetails")).map(map->map.getValue()).collect(Collectors.toList()));
Struck here , unable to loop / parse further .
how to get the collegeLoc value in this case ? any help would be great .
答案1
得分: 1
尝试以下代码:
empMap
.entrySet()
.stream()
.filter(map -> map.getKey().equals("CDetails"))
.forEach(e -> {
JSONArray jsArray = (JSONArray) e.getValue();
for (Object jsObject : jsArray) {
JSONObject obj = (JSONObject) jsObject;
if (obj.containsKey("collegeLoc")) {
System.out.println(obj.getAsString("collegeLoc"));
}
}
});
英文:
Try the following code,
empMap
.entrySet()
.stream()
.filter(map -> map.getKey().equals("CDetails"))
.forEach(e -> {
JSONArray jsArray = (JSONArray) e.getValue();
for (Object jsObject : jsArray) {
JSONObject obj = (JSONObject) jsObject;
if (obj.containsKey("collegeLoc")) {
System.out.println(obj.getAsString("collegeLoc"));
}
}
});
答案2
得分: 0
可能是这样的:
json.getJSONArray("CDetails").getJSONObject(0).getString("slogan")
或者,更好的做法是创建一个Value Object并对其进行序列化/反序列化。使用Java类而不是纯JSON文档更容易操作和导航。
class ValueObject {
@JsonProperty("CDetails")
private List<Detail> details;
static class Detail {
private String collegeName;
private String collegeLoc;
}
}
英文:
It might be
json.getJSONArray("CDetails").getJSONObject(0).getString("slogan")
Or, which is better, create a Value Object and serialize / deserialize it. It will be much easier to manipulate & navigate with Java class rather than with a pure JSON document.
class ValueObject {
@JsonProperty("CDetails")
private List<Detail> details;
static class Detail {
private String collegeName;
private String collegeLoc;
}
}
答案3
得分: 0
使用 net.minidev.json
JSON 库时,请尝试以下代码:
JSONArray array = (JSONArray) empMap.get("CDetails");
array.stream().filter(o -> {
JSONObject jsonObject = (JSONObject) o;
return jsonObject.containsKey("collegeLoc");
}).map(o -> {
JSONObject jsonObject = (JSONObject) o;
return jsonObject.getAsString("collegeLoc");
}).forEach(System.out::println);
英文:
If use net.minidev.json
json lib,try the follow code:
JSONArray array = (JSONArray) empMap.get("CDetails");
array.stream().filter(o -> {
JSONObject jsonObject = (JSONObject) o;
return jsonObject.containsKey("collegeLoc");
}).map(o -> {
JSONObject jsonObject = (JSONObject) o;
return jsonObject.getAsString("collegeLoc");
}).forEach(System.out::println);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论