从对象数组中获取或提取值,从哈希映射中获取

huangapple go评论105阅读模式
英文:

getting or fetching the Value from an Object Array , from an HashMap

问题

我已经得到了以下的映射结构

{CDetails=[{"collegeName":"Peters Stanford","collegeLoc":"UK"},{}]}

我正在尝试从上述提到的结构中获取 collegeLoc 的值

这是我尝试过的

  1. public static void main(String[] args) {
  2. Map<String, Object> empMap = new HashMap<>();
  3. JSONObject collegeNameJsonObj = new JSONObject();
  4. collegeNameJsonObj.put("collegeName", "Peters Stanford");
  5. JSONObject collegeIdJsonObj = new JSONObject();
  6. collegeNameJsonObj.put("collegeLoc", "UK");
  7. JSONArray jsonArray = new JSONArray();
  8. jsonArray.add(collegeNameJsonObj);
  9. jsonArray.add(collegeIdJsonObj);
  10. empMap.put("CDetails", jsonArray);
  11. 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

  1. public static void main(String[] args) {
  2. Map &lt; String, Object &gt; empMap = new HashMap &lt; &gt; ();
  3. JSONObject collegeNameJsonObj = new JSONObject();
  4. collegeNameJsonObj.put(&quot;collegeName&quot;, &quot;Peters Stanford&quot;);
  5. JSONObject collegeIdJsonObj = new JSONObject();
  6. collegeNameJsonObj.put(&quot;collegeLoc&quot;, &quot;UK&quot;);
  7. JSONArray jsonArray = new JSONArray();
  8. jsonArray.add(collegeNameJsonObj);
  9. jsonArray.add(collegeIdJsonObj);
  10. empMap.put(&quot;CDetails&quot;, jsonArray);
  11. System.out.println(empMap.entrySet().stream().filter(map-&gt;map.getKey().equals(&quot;CDetails&quot;)).map(map-&gt;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

尝试以下代码:

  1. empMap
  2. .entrySet()
  3. .stream()
  4. .filter(map -> map.getKey().equals("CDetails"))
  5. .forEach(e -> {
  6. JSONArray jsArray = (JSONArray) e.getValue();
  7. for (Object jsObject : jsArray) {
  8. JSONObject obj = (JSONObject) jsObject;
  9. if (obj.containsKey("collegeLoc")) {
  10. System.out.println(obj.getAsString("collegeLoc"));
  11. }
  12. }
  13. });
英文:

Try the following code,

  1. empMap
  2. .entrySet()
  3. .stream()
  4. .filter(map -&gt; map.getKey().equals(&quot;CDetails&quot;))
  5. .forEach(e -&gt; {
  6. JSONArray jsArray = (JSONArray) e.getValue();
  7. for (Object jsObject : jsArray) {
  8. JSONObject obj = (JSONObject) jsObject;
  9. if (obj.containsKey(&quot;collegeLoc&quot;)) {
  10. System.out.println(obj.getAsString(&quot;collegeLoc&quot;));
  11. }
  12. }
  13. });

答案2

得分: 0

可能是这样的:

  1. json.getJSONArray("CDetails").getJSONObject(0).getString("slogan")

或者,更好的做法是创建一个Value Object并对其进行序列化/反序列化。使用Java类而不是纯JSON文档更容易操作和导航。

  1. class ValueObject {
  2. @JsonProperty("CDetails")
  3. private List<Detail> details;
  4. static class Detail {
  5. private String collegeName;
  6. private String collegeLoc;
  7. }
  8. }
英文:

It might be

  1. json.getJSONArray(&quot;CDetails&quot;).getJSONObject(0).getString(&quot;slogan&quot;)

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.

  1. class ValueObject {
  2. @JsonProperty(&quot;CDetails&quot;)
  3. private List&lt;Detail&gt; details;
  4. static class Detail {
  5. private String collegeName;
  6. private String collegeLoc;
  7. }
  8. }

答案3

得分: 0

使用 net.minidev.json JSON 库时,请尝试以下代码:

  1. JSONArray array = (JSONArray) empMap.get("CDetails");
  2. array.stream().filter(o -> {
  3. JSONObject jsonObject = (JSONObject) o;
  4. return jsonObject.containsKey("collegeLoc");
  5. }).map(o -> {
  6. JSONObject jsonObject = (JSONObject) o;
  7. return jsonObject.getAsString("collegeLoc");
  8. }).forEach(System.out::println);
英文:

If use net.minidev.json json lib,try the follow code:

  1. JSONArray array = (JSONArray) empMap.get(&quot;CDetails&quot;);
  2. array.stream().filter(o -&gt; {
  3. JSONObject jsonObject = (JSONObject) o;
  4. return jsonObject.containsKey(&quot;collegeLoc&quot;);
  5. }).map(o -&gt; {
  6. JSONObject jsonObject = (JSONObject) o;
  7. return jsonObject.getAsString(&quot;collegeLoc&quot;);
  8. }).forEach(System.out::println);

huangapple
  • 本文由 发表于 2020年5月5日 10:51:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61604925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定