Java 8: How to write lambda stream to work with JsonArray?

后端 未结 3 1020
谎友^
谎友^ 2020-12-17 21:26

I\'m very new to Java 8 lambdas and stuff... I want to write a lambda function that takes a JsonArray, goes over its JsonObjects and creates a list of values of certain fiel

相关标签:
3条回答
  • 2020-12-17 22:00

    Yes, I recognized that result of put is being added to json array instead of json object. Should be like this.

    private static JSONObject getJson(String key, String value){
        JSONObject result = new JSONObject();
        result.put(key, value);
        return result;        
    }
    
    public static void main(String[] args) {
        JSONArray jsonArray = new JSONArray();
    
        jsonArray.add(getJson("name", "John"));
        jsonArray.add(getJson("name", "David"));
    
        List list = (List) jsonArray.stream()
                    .map(json -> json.toString())
                    .collect(Collectors.toList());
        System.out.println(list);
    }
    

    and output is now [{"name":"John"}, {"name":"David"}] like expected

    0 讨论(0)
  • 2020-12-17 22:01

    JSONArray is a sub-class of java.util.ArrayList and JSONObject is a sub-class of java.util.HashMap.

    Therefore, new JSONObject().put("name", "John") returns the previous value associated with the key (null), not the JSONObject instance. As a result, null is added to the JSONArray.

    This, on the other hand, works:

        JSONArray jsonArray = new JSONArray();
        JSONObject j1 = new JSONObject();
        j1.put ("name", "John");
        JSONObject j2 = new JSONObject();
        j2.put ("name", "David");
        jsonArray.add(j1);
        jsonArray.add(j2);
        Stream<String> ss = jsonArray.stream().map (json->json.toString ());
        List<String> list = ss.collect (Collectors.toList ());
        System.out.println(list);
    

    For some reason I had to split the stream pipeline into two steps, because otherwise the compiler doesn't recognize that .collect (Collectors.toList()) returns a List.

    The output is:

    [{"name":"John"}, {"name":"David"}]
    
    0 讨论(0)
  • 2020-12-17 22:16

    Try with IntStream.

    List<String> jsonObject = IntStream
           .range(0,jsonArray.size())
           .mapToObj(i -> jsonArray.getJSONObject(i))
           .collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题