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

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

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 &lt; String, Object &gt; empMap = new HashMap &lt; &gt; ();
     
    JSONObject collegeNameJsonObj = new JSONObject();
    collegeNameJsonObj.put(&quot;collegeName&quot;, &quot;Peters Stanford&quot;);
    
    JSONObject collegeIdJsonObj = new JSONObject();
    collegeNameJsonObj.put(&quot;collegeLoc&quot;, &quot;UK&quot;);
    
    JSONArray jsonArray = new JSONArray();
    jsonArray.add(collegeNameJsonObj);
    jsonArray.add(collegeIdJsonObj);
   
    empMap.put(&quot;CDetails&quot;, jsonArray);
  
    
  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

尝试以下代码:

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 -&gt; map.getKey().equals(&quot;CDetails&quot;))
    .forEach(e -&gt; {
            JSONArray jsArray = (JSONArray) e.getValue();
            for (Object jsObject : jsArray) {
                JSONObject obj = (JSONObject) jsObject;
                if (obj.containsKey(&quot;collegeLoc&quot;)) {
                    System.out.println(obj.getAsString(&quot;collegeLoc&quot;));
                }
            }
    });

答案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(&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.

class ValueObject {
  @JsonProperty(&quot;CDetails&quot;)
  private List&lt;Detail&gt; 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(&quot;CDetails&quot;);

array.stream().filter(o -&gt; {
	JSONObject jsonObject = (JSONObject) o;
	return jsonObject.containsKey(&quot;collegeLoc&quot;);
}).map(o -&gt; {
	JSONObject jsonObject = (JSONObject) o;
	return jsonObject.getAsString(&quot;collegeLoc&quot;);
}).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:

确定